【问题标题】:Change sender name to value from order meta data in WooCommerce email notifications在 WooCommerce 电子邮件通知中将发件人名称更改为订单元数据中的值
【发布时间】:2021-12-25 00:11:24
【问题描述】:

我正在尝试根据订单元数据更改 WooCommerce 发件人电子邮件名称。

该网站是一个多供应商市场,因此每个订单都包含元数据以及客户订购的商家名称。因此,当订单状态更改为处理中时,我要做的是从订单元数据中获取企业名称,并将发件人电子邮件名称设置为该元数据。这样,客户在下订单时会在收到的电子邮件中看到公司名称。

这是我尝试过的,但不幸的是没有得到想要的结果。有什么建议吗?

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

【问题讨论】:

    标签: php wordpress woocommerce email-notifications


    【解决方案1】:

    当订单状态变为处理中时,无需在woocommerce_order_status_processing 挂钩中添加此过滤器。因为我们可以反过来应用。通过$email->id,可以在触发此挂钩时针对特定的电子邮件通知。

    要获取属于订单的元数据,可以使用$order->get_meta( 'meta_key' )

    所以你得到:

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

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-12
      • 2018-02-17
      • 2018-02-09
      相关资源
      最近更新 更多