简体   繁体   English

更改 woocommerce 发件人电子邮件名称动态

[英]Change woocommerce sender email name dynamic

I'm trying to change WooCommerce sender email name based on the order meta.我正在尝试根据订单元更改 WooCommerce 发件人电子邮件名称。

The website is a multi vendor marketplace, so each order contains meta data with the business name to which the customer orders.该网站是一个多供应商市场,因此每个订单都包含带有客户订购的企业名称的元数据。 So when the order status changes to processing, what I am trying to do is get the business name from the order meta, and set the sender email name to that meta.因此,当订单状态更改为处理时,我要做的是从订单元中获取企业名称,并将发件人电子邮件名称设置为该元。 This way, the customer will see the business name in the email he receives when placing an order.这样,客户将在下订单时收到的电子邮件中看到企业名称。

This is what I've tried, but it does not seem to be working.这是我尝试过的,但似乎不起作用。

function change_processing_sender_name($order_id){
    $merchant_id = get_post_meta( $order_id, 'merchant_id', true );
    $business_name = get_user_meta($merchant_id,'businessname', true);
    add_filter( 'woocommerce_email_from_name', function( $from_name, $wc_email ){
        $from_name = $business_name;
        return $from_name;
    }, 10 ,2);
}
add_action( 'woocommerce_order_status_processing', 'change_processing_sender_name' );

Any help appreciated!任何帮助表示赞赏!

It is not necessary to add this filter in the woocommerce_order_status_processing hook, when the order status changes to processing.当订单状态更改为 processing 时, woocommerce_order_status_processingwoocommerce_order_status_processing挂钩中添加此过滤器。 Because we can apply the reverse.因为我们可以反过来应用。 Via $email->id it is possible to target specific email notifications when this hook is triggered.通过$email->id可以在触发此钩子时针对特定的电子邮件通知。

To obtain meta data belonging to the order, you can use $order->get_meta( 'meta_key' )要获取属于订单的元数据,您可以使用$order->get_meta( 'meta_key' )

So you get:所以你得到:

function filter_woocommerce_email_from_name( $from_name, $email ) {
    // Targeting specific email notification via the email id
    if ( $email->id == 'customer_processing_order' && is_a( $email->object, 'WC_Order' ) ) {
        // Get order
        $order = $email->object;
        
        // Get meta
        $business_name = $order->get_meta( 'businessname' );

        // NOT empty
        if ( ! empty ( $business_name  ) ) {
            $from_name = $business_name;
        }
    }

    return $from_name;
}
add_filter( 'woocommerce_email_from_name', 'filter_woocommerce_email_from_name', 10, 2 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM