Woocommerce使用产品ID获取供应商信息 [英] Woocommerce Get Vendor Information with Product ID

查看:65
本文介绍了Woocommerce使用产品ID获取供应商信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Woo Commerce与WC Vendor和WC Booking插件一起使用.我想发送预订通知给供应商.目前,它会向客户和管理员发送通知,而当管理员将产品状态更改为处理中"时,它将发送通知完成,然后将通知发送给供应商.但是,我想将供应商通知与管理员通知一起发送.

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.

尝试了这个钩子:

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

因此,现在,我尝试在此中直接钩住供应商电子邮件以及客户和管理员电子邮件:

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

文件中:

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

在此阶段,我有可用的可预订产品ID,我正在尝试使用产品ID提取供应商信息,但是我没有找到任何信息.

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.

尝试:

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

问题是:如何使用产品ID获取供应商信息(特别是电子邮件)?

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

推荐答案

我没有,而且我从未使用过WC Vendors插件,因为这是一个非官方的商业插件(不是自动制作的).

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

要获取供应商ID(稍作搜索后),您可以通过以下方式从产品ID中获取它:

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;


可能是个不错的选择:

您可以使用专用的过滤器挂钩 woocommerce_email_recipient _ {$ this-> id} ,其中 $ this-> id 是通知类型的ID,对于 new_booking 电子邮件ID(也用于测试 new_order 电子邮件ID).

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.

在电子邮件通知挂钩中,几乎总是定义Order对象.因为在一个订单中您可以有很多物品(不同供应商的不同产品),所以您需要从每个供应商那里获取供应商ID.

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.

在下面的代码中,我向收件人添加用于 new_booking new_order 电子邮件通知的供应商电子邮件:

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

代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

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

这应该可以正常工作.

这篇关于Woocommerce使用产品ID获取供应商信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆