为 Woocommerce 中的特定运输方式发送订单保留电子邮件

Send Order On Hold Email for a specific shipping method in Woocommerce

我使用的是 Wordpress 4.9.6 和 WooCommerce 3.4.3 版,我需要发送 'Order on Hold' 电子邮件了解特定的送货方式。

原因? 我使用 DHL shipping plugin 来计算运费,也可以使用 'Alternate' 运输方式。如果用户在结帐时选择 DHL 运输,则计算运费并下单即可。但是,如果他们选择 'Alternate' 送货方式,我必须通知他们他们的订单被搁置,直到他们支付运费,因为 'Alternate' 方式被重命名为 'Free Shipping' 我会发出他们在订购后单独开具发票以支付运费。

在寻找我的问题的解决方案时,我在这个回答线程中找到了一些符合我需要的代码:

但我无法弄清楚如何编辑此代码以使其适用于我的特定场景。

非常感谢您的帮助。

要使其适用于重命名的免费送货方式,您需要稍微更改代码:

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 "On hold" email notification and "Free Shipping" Shipping Method
    if ( 'customer_on_hold_order' == $email->id && $order->has_shipping_method('free_shipping') ){
        $order_id = $order->get_id(); // The Order ID

        // Your message output
        echo "<h2>Shipping notice</h2>
        <p>Your custom message goes here… your custom message goes here… your custom message goes here… your custom message goes here… your custom message goes here…</p>";
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。


强制 "On hold" 和 "Completed" 电子邮件通知 (可选)

在订单状态更改时,以下代码将触发 "On hold" 电子邮件通知,仅针对您重命名的 "Free shipping" 送货方式和 "Completed" 电子邮件通知。

add_action( 'woocommerce_order_status_changed', 'sending_on_hold_email_notification', 20, 4 );
function sending_on_hold_email_notification( $order_id, $old_status, $new_status, $order ){
    // Only  "On hold" order status and "Free Shipping" Shipping Method
    if ( $order->has_shipping_method('free_shipping') && $new_status == 'on-hold' ){
        // Getting all WC_emails objects
        $notifications = WC()->mailer()->get_emails();
        // Send "On hold" email notification
        $notifications['WC_Email_Customer_On_Hold_Order']->trigger( $order_id );
    } elseif ( ! $order->has_shipping_method('free_shipping') && $new_status == 'completed' ){
        // Getting all WC_emails objects
        $notifications = WC()->mailer()->get_emails();
        // Send "On hold" email notification
        $notifications['WC_Email_Customer_Completed_Order']->trigger( $order_id );
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。