当我配置WooCommerce'新订单'电子邮件设置,我在收件人字段中写了两封电子邮件(email1 @ domain.com,email2 @ domain.com)。
我的需求是: - 当用户购买可下载产品时,发送电子邮件至email1 - 当用户购买普通产品时,请发送电子邮件至email1和email2
我已经查看了WooCommerce的代码,并且无法实现函数或某些代码来执行此操作...
有人能给我一些线索或帮助吗?
谢谢!
答案 0 :(得分:3)
经过测试的代码:
add_filter( 'woocommerce_email_recipient_new_order', 'so_28025177', 10, 2 );
function so_28025177( $recipients, $order_object ) {
$email_one = 'downloadable@example.com';
$email_two = 'physical@example.com';
$wc_order = new WC_Order( $order_object->id );
$order_items = $wc_order->get_items( );
$has_downloadable = false;
$has_physical = false;
foreach( $order_items as $item ) {
$product_id = $item[ 'product_id' ];
$product_meta = get_post_meta( $product_id, '_downloadable', true );
if( $product_meta == 'yes' ) {
$has_downloadable = true;
}
elseif( $product_meta == 'no' ) {
$has_physical = true;
}
}
if( $has_downloadable && ! $has_physical ) {
$recipients .= ',' . $email_one;
}
elseif ( $has_physical && ! $has_downloadable ) {
$recipients .= ',' . $email_two;
}
elseif ( $has_downloadable && $has_physical ) {
$recipients .= ',' . $email_one . ',' . $email_two;
}
return $recipients;
}