根据 WooCommerce 客户完成订单电子邮件通知中的送货方式 ID 添加消息

问题描述 投票:0回答:2

我想根据 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 个不同的本地取货

  • 本地取货:10
  • 本地取货:11

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

wordpress woocommerce hook-woocommerce email-notifications shipping-method
2个回答
4
投票

要获取送货方式 ID,您可以使用

$shipping_item->get_method_id()
+
$shipping_item->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 );

0
投票

对于没有运费的订单,有没有办法省略此功能?目前,如果订单没有发货并转到“已完成”,则该函数会产生错误:

致命错误:未捕获错误:调用 bool 上的成员函数 get_method_id() 堆栈跟踪:#0 wp-includes/class-wp-hook.php(324):action_woocommerce_email_order_details() #1 wp-includes/class-wp-hook.php(348):WP_Hook->apply_filters() #2 wp-包含/plugin.php(517): WP_Hook->do_action() #3 customer-completed-order.php(38): do_action() #4 woocommerce/includes/wc-core-functions.php(345): include('/var/www/vhosts...')

© www.soinside.com 2019 - 2024. All rights reserved.