您正在使用
wc_print_notice
在循环中,它将显示与循环计数相同的时间。为了克服这个问题,请尝试使用以下代码。
替换这段代码。此代码将默认禁用通知调用。只有在执行循环并删除购物车项目时,它才会启用通知调用。
// Loop through cart items
foreach ( $cart_items as $cart_item_key => $cart_item ) {
// Remove all others cart items when specific product ID is the last added to cart
if ( ! in_array($specific_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) && $is_last_item ) {
$cart->remove_cart_item( $cart_item_key );
wc_print_notice( 'Individual resources have been removed as the full pack already contains all the resources', 'notice' );
}
}
用这个密码告诉我结果。
// Loop through cart items
$enable_notice = false;
foreach ( $cart_items as $cart_item_key => $cart_item ) {
// Remove all others cart items when specific product ID is the last added to cart
if ( ! in_array($specific_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) && $is_last_item ) {
$cart->remove_cart_item( $cart_item_key );
$enable_notice = true;
}
}
if($enable_notice){
wc_print_notice( 'Individual resources have been removed as the full pack already contains all the resources', 'notice' );
}