我使用的是 Wordpress 4.9.6 和 WooCommerce 3.4.3 版,我需要针对特定的送货方式发送“保留顺序”电子邮件。
原因? 我使用 DHL shipping plugin 来计算运费,并且还可以使用“替代”运输方式。如果用户在结帐时选择 DHL 运输,则计算运费并下单即可。但是,如果他们选择“替代”送货方式,我必须通知他们他们的订单被搁置,直到他们支付运费,因为“替代”方式被重命名为“免费送货”,我会为他们开具单独的发票下单后支付运费。
在寻找我的问题的解决方案时,我在这个回答线程中找到了一些符合我需要的代码:Customizing Woocommerce New Order email notification based on shipping method
但我无法弄清楚如何编辑此代码以使其适用于我的特定场景。
非常感谢您的帮助。
最佳答案
要使其适用于重命名的免费送货方式,您需要稍微更改代码:
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 文件。测试和工作。
强制“暂停”和“完成”电子邮件通知(可选)
在订单状态更改时,以下代码将仅针对您重命名的“免费送货”送货方式和“已完成”电子邮件通知触发“暂停”电子邮件通知。
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 文件。测试和工作。
关于php - 为 Woocommerce 中的特定运输方式发送订单保留电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51007318/