我想从我的订单通知(通过电子邮件发送)中隐藏一种产品
我以这段代码为基础,它有效。此代码使用产品 ID 隐藏结帐页面中的某些产品:
function filter_woocommerce_cart_item_visible( $true, $cart_item, $cart_item_key ) {
// The targeted product ids
$targeted_ids = array( 5500, 5499, 5498 );
// Computes the intersection of arrays
if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$true = false;
}
return $true;
}
add_filter( 'woocommerce_cart_item_visible', 'filter_woocommerce_cart_item_visible', 10, 3 );
我尝试更改代码以在订单通知中隐藏此产品(通过订单项),但修改后的代码会引发错误。这是代码:
function hide_product_from_order( $true, $order_item, $order_item_key ) {
// The targeted product ids
$targeted_ids = array( 5500, 5499, 5498 );
// Computes the intersection of arrays
if ( array_intersect( $targeted_ids, array( $order_item['product_id'],$order_item['variation_id'] ) ) ) {
$true = false;
}
return $true;
}
add_filter( 'woocommerce_order_item_visible', 'hide_product_from_order', 10, 3 );
它们是您的代码中的一些错误,请尝试以下修改后的代码:
add_filter( 'woocommerce_order_item_visible', 'hide_specific_order_items', 10, 2 );
function hide_specific_order_items( $visible, $item ) {
// The targeted product ids to hide
$targeted_ids = array( 5500, 5499, 5498 );
// Hide matching order item(s)
if ( array_intersect( $targeted_ids, array( $item->get_product_id(), $item->get_variation_id() ) ) ) {
$visible = false;
}
return $visible;
}
代码位于活动子主题(或活动主题)的functions.php 文件中。现在应该可以工作了。