Woocommerce Get Vendor Information with Product ID

南楼画角 提交于 2021-02-04 16:11:06

问题


I am using Woo Commerce with WC Vendor and WC Booking plugin. I want to send booking notification to vendor. Currently it sends notification to Customer and Administrator and when admin changes product status to processing & completed, then it sends notification to vendor. However, I want to send vendor notification along with admin notification.

I tried this hook:

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 );
    }

}

But it throws an error:

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

So, now I am trying to hook vendor email directly along with customer and administrator email in this line:

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

in file:

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

At this stage, I have bookable product id available and I am trying to pull vendor information using product id but I don't found any information.

I tried:

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

The Question is: How to get vendor information (specifically email) using product id?


回答1:


I don't have and I have never used WC Vendors plugin as this is a non official commercial plugin (not made by automatic).

To get the vendor ID (after searching a bit) you can get it this way from a product ID:

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

Now you can get the vendor email this way:

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

May be a good turn around:

You can use the dedicated filter hook woocommerce_email_recipient_{$this->id} where $this->id is the ID of the notification type, for new_booking email ID (and also for testing new_order email ID too).

This will allow you to add additional email recipients.

In email notifications hooks, the Order object is nearly always defined. As In an order you can have many items (different products rom different vendors), you will need to get the vendor ID from each.

In the code below I add to the recipients the vendors emails for new_booking and new_order email notifications:

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 ) {
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;

    $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;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This should work without errors.



来源:https://stackoverflow.com/questions/47604737/woocommerce-get-vendor-information-with-product-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!