当我配置WooCommerce的'新订单'电子邮件设置时,我在收件人字段中写了两封电子邮件(email1@domain.com,email2@domain.com)。
我的需求是:- 当用户购买可下载产品时,发送电子邮件到电子邮件1- 当用户购买普通产品时,发送电子邮件到电子邮件1和电子邮件2
我已经浏览了WooCommerce的代码,并且不可能实现一个函数或一些代码来做到这一点......
任何人都可以给我一个线索或帮助来做到这一点,请?
谢谢!
测试代码:
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;
}