我正在尝试根据送货方式 ID 向我们的其中一个送货地点发送一封额外的电子邮件通知。我找到了这个答案代码,但我无法以正确的方式更改它,以使其适用于我的运输方式:
add_filter( 'woocommerce_email_recipient_new_order', 'new_order_additional_recipients', 20, 2 );
function new_order_additional_recipients( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
// Set Below your email adresses
$email1 = '[email protected]';
$email2 = '[email protected]';
// 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();
// Adding recipients conditionally
if ( 'flat_rate:8' == $shipping_method_id )
$recipient .= ',' . $email1;
elseif ( 'flat_rate:9' == $shipping_method_id )
$recipient .= ',' . $email2;
return $recipient;
}
这是我尝试使用的方法,但由于某种原因,我没有收到根据方法 ID 发送给收件人的电子邮件。我是否需要在代码中添加订单处理才能触发此操作?也许是这样的:
if('customer_processing_order' !=$email_id );
if('flat_rate:8' == $shipping_method_id );
我将非常感谢您的帮助。
代码有效,因此您没有获得正确的段…现在,由于每个运输实例 ID 都是唯一的,您可以尝试以下操作:
add_filter( 'woocommerce_email_recipient_new_order', 'new_order_additional_recipients', 20, 2 );
function new_order_additional_recipients( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
// Set Below your email adresses
$email1 = '[email protected]';
$email2 = '[email protected]';
// Get the shipping method Id
$shipping_items = $order->get_items('shipping');
$shipping_item = reset($shipping_items);
// Adding recipients conditionally
if ( $shipping_item->get_instance_id() == 8 ){
$recipient .= ',' . $email1;
} elseif( $shipping_item->get_instance_id() == 9 ){
$recipient .= ',' . $email2;
}
return $recipient;
}
应该有效