【问题标题】:Add message based on shipping method ID in WooCommerce customer completed order email notification在 WooCommerce 客户完成订单电子邮件通知中添加基于运输方式 ID 的消息
【发布时间】:2021-12-23 08:32:55
【问题描述】:

我想在 WooCommerce 客户完成订单电子邮件通知中添加一条基于送货方式 ID 的消息。

我正在使用以下代码:

add_action ('woocommerce_email_order_details', 'custom_email_notification_for_shipping', 5, 4);
function custom_email_notification_for_shipping( $order, $sent_to_admin, $plain_text, $email ){

    // Only for "completed" email notification and "Local Pickup" Shipping Method
    if ( 'customer_completed_order' == $email->id && $order->has_shipping_method('local_pickup') ){
        $order_id = $order->get_id(); // The Order ID

        // Your message output
        echo "<h2>TEST</h2>
        <p>Shipping info test</p>";
    }
}

这可行,但我有 2 个不同的本地取货

  • local_pickup:10
  • local_pickup:11

是否可以将其设置为仅 local_pickup:10?

【问题讨论】:

    标签: wordpress woocommerce hook-woocommerce email-notifications shipping-method


    【解决方案1】:

    要获取运输方式ID,您可以使用$shipping_item-&gt;get_method_id() + $shipping_item-&gt;get_instance_id() 并连接字符串

    所以你得到:

    function action_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
        // Target specific email
        if ( $email->id == 'customer_completed_order' ) {
            // Get the shipping method ID
            $shipping_items      = $order->get_items( 'shipping' );
            $shipping_item       = reset( $shipping_items );
            $shipping_method_id  = $shipping_item->get_method_id() . ':';
            $shipping_method_id .= $shipping_item->get_instance_id();
             
            // Compare
            if ( $shipping_method_id == 'local_pickup:1' ) {
                echo 'test 1';
            } elseif ( $shipping_method_id == 'local_pickup:10' ) {
                echo 'test 2';
            } elseif ( $shipping_method_id == 'local_pickup:11' ) {
                echo 'test 3';
            } else {
                echo 'DEBUG INFORMATION = ' . $shipping_method_id;
            }
         }
    }
    add_action ('woocommerce_email_order_details', 'action_woocommerce_email_order_details', 10, 4 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-02
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 2019-04-16
      • 2019-01-17
      • 2021-07-20
      • 1970-01-01
      相关资源
      最近更新 更多