问题描述
遵循此代码示例 Disable WooCommerce email notification for specific product (我还不能在那里发表评论)。我实现了为隔离产品运行的代码,我的意思是只有当产品在订单($product_id == 5274
)中时,此代码才有效:
add_filter('woocommerce_email_recipient_new_order','remove_free_course_notifications',10,2);
function remove_free_course_notifications( $recipient,$order )
{
$page = $_GET['page'] = isset($_GET['page']) ? $_GET['page'] : '';
if ('wc-settings' === $page) {
return $recipient;
}
if (!$order instanceof WC_Order) {
return $recipient;
}
//my product id is 5274
$items = $order->get_items();
foreach ($items as $item) {
$product_id = $item['product_id'];
if ($product_id == 5274) {
$recipient = '';
}
return $recipient;
}
}
解决方法
这段代码有点过时了。请改用以下内容,以停止针对“独家”定义产品的新订单管理员通知:
add_filter( 'woocommerce_email_recipient_new_order','remove_free_course_notifications',10,2 );
function remove_free_course_notifications( $recipient,$order )
{
if ( ! is_a( $order,'WC_Order' ) ) {
return $recipient;
}
$targeted_product_id = 5274; // Here set your product ID
$other_found = $product_found = false;
foreach ( $order->get_items() as $item ) {
$product_id = $item->get_product_id();
if ( $item->get_product_id() == $targeted_product_id ) {
$product_found = true;
} else {
$other_found = true;
}
}
if ( $product_found && ! $other_found ) {
return '';
}
return $recipient;
}
代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。