3

我正在使用 Wordpress 4.9.6 和 WooCommerce 3.4.3 版本,我需要发送“订单暂挂”电子邮件以获取特定的运输方式。

原因?我使用 DHL 运输插件来计算运输,并且还可以使用“替代”运输方式。如果用户在结账时选择 DHL 运输,则计算运费并下单。但是,如果他们选择“替代”运输方式,我必须通知他们他们的订单已暂停,直到他们支付运费,因为“替代”方式已重命名为“免费送货”,我将为他们开具单独的发票订购后支付运费。

在寻找我的问题的解决方案时,我在这个答案线程中找到了一些符合我需求的代码:Customizing Woocommerce New Order email notification based on shipping method

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

非常感谢您的帮助。

4

1 回答 1

3

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

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…&lt;/p>";
    }
}

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

在此处输入图像描述


强制“暂停”和“已完成”电子邮件通知(可选)

在订单状态更改时,以下代码将仅针对您重命名的“免费送货”送货方式和“已完成”电子邮件通知触发“暂停”电子邮件通知。

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 文件中。测试和工作。

于 2018-06-24T10:28:28.637 回答