2017-12-02 12 views
0

私はWoo CommerceをWC VendorとWC Bookingプラグインと共に使用しています。ベンダーに予約通知を送信したいと思います。現時点では、お客様および管理者に通知を送信し、管理者が製品ステータスを処理&に変更すると、通知がベンダーに送信されます。しかし、私は管理者通知と共にベンダー通知を送信したい。Woocommerce製品IDを持つベンダー情報を取得する

add_action('woocommerce_new_booking', 'new_order_email_to_vendor', 10, 4); 
function new_order_email_to_vendor($order){ 

    $emails = WC()->mailer()->get_emails(); 

    if (! empty($emails)) { 
     $emails['WC_Product_Vendors_Order_Email_To_Vendor']->trigger($order); 
    } 

} 

をしかし、それはエラーがスローされます:

私はは、このフックを試してみましたので、

Fatal error: Call to a member function get_date_created() on boolean in /wp-content/plugins/woocommerce-product-vendors/includes/emails/class-wc-product-vendors-order-email-to-vendor.php on line 56 

を、今私が直接顧客と管理者の電子メールと一緒にベンダーの電子メールをフックしようとしています

$this->recipient  = $this->get_option('recipient', get_option('admin_email'), 'WILL ADD VENDOR EMAIL HERE'); 

ファイルに:この段階で

wp-content/plugins/woocommerce-bookings/includes/emails/class-wc-email-new-booking.php 

、私は予約可能な製品IDを用意して、私は、製品IDを使用して、ベンダーの情報を引き出すためにしようとしていますが、私はすべての情報を見つけていません。

私はを試してみました:

  • get_post()
  • get_post_meta()
  • get_post_meta_by_id()

質問です:製品IDを使用してベンダー情報(特にメール)を取得する方法は?

答えて

1

私は持っていません。これは公式ではない市販のプラグイン(自動メイドではありません)としてWCベンダープラグインを使用したことがありません。

あなたは、製品IDから、このようにそれを得ることができます(ビットを検索した後)のベンダIDを取得するには:

$vendor_id = get_post_field('post_author', $product_id); 

今、あなたは、ベンダーの電子メールをこのように得ることができます。

$vendor_id = get_post_field('post_author', $product_id); 
$vendor = get_userdata($vendor_id); 
$email = $vendor->user_email; 

は周りの良いターンである場合があります。

あなたが目を使用することができますe専用フィルターフックwoocommerce_email_recipient_{$this->id} 電子メールID(およびまたnew_order電子メールIDもテストします)の場合、$this->idが通知タイプのIDです。

メール受信者を追加することができます。

電子メール通知フックでは、Orderオブジェクトはほぼ常に定義されています。 Asあなたが多くのアイテム(異なる製品は異なるベンダーを持つ)を持つことができるオーダーでは、それぞれからベンダーIDを取得する必要があります。

add_filter('woocommerce_email_recipient_new_booking', 'additional_customer_email_recipient', 10, 2); 
add_filter('woocommerce_email_recipient_new_order', 'additional_customer_email_recipient', 10, 2); // Optional (testing) 
function additional_customer_email_recipient($recipient, $order) { 
    $additional_recipients = array(); // Initializing… 

    // Iterating though each order item 
    foreach($order->get_items() as $item_id => $line_item){ 
     // Get the vendor ID 
     $vendor_id = get_post_field('post_author', $line_item->get_product_id()); 
     $vendor = get_userdata($vendor_id); 
     $email = $vendor->user_email; 

     // Avoiding duplicates (if many items with many emails) 
     // or an existing email in the recipient 
     if(! in_array($email, $additional_recipients) && strpos($recipient, $email) === false) 
      $additional_recipients[] = $email; 
    } 

    // Convert the array in a coma separated string 
    $additional_recipients = implode(',', $additional_recipients); 

    // If an additional recipient exist, we add it 
    if(count($additional_recipients) > 0) 
     $recipient .= ','.$additional_recipients; 

    return $recipient; 
} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルに入るか、また、任意で:私はnew_bookingnew_order電子メール通知のためのベンダーの電子メール受信者に追加するには、以下のコードでは

プラグインファイル。

これはエラーなしで動作するはずです。

+0

最初のものは電子メールIDを返しますが、管理者が製品を作成したため管理者の電子メールです。これは制限事項です。それを回避する方法はありますか? 2番目のメソッドにエラーがあります。 '致命的なエラー:未定義のメソッドを呼び出すWC_Booking :: get_items()' – Alena

関連する問題