我有一个功能正常工作,除了它在购物车中显示通知两次而不是一次。该功能应用折扣并显示通知。该函数查找特定类别的商品并添加总数,如果满足折扣金额,则应用折扣并显示通知。现在,即使购物车中只有一件商品,它也会显示两次通知。
我尝试过添加wc_clear_notices()
但这将清除我需要的其他通知,例如类别最小订单金额的最小最大通知。如果我添加wc_clear_notices()
在函数的开头或任何 foreach 语句中,它会在显示之前清除其他最小/最大通知。
这是我已经看过的一些问题,但只是说使用wc_clear_notices()
这不起作用,因为其他通知被清除:
WooCommerce 注意,仅运行一次 https://stackoverflow.com/questions/40405988/woocommerce-notice-run-only-once
仅显示一次 wc_add_notice https://stackoverflow.com/questions/43979665/show-wc-add-notice-only-one-time
这是我目前拥有的代码,可以正确地对商品进行折扣并显示通知,但显示通知两次而不是一次:
//apply discounts to foil and seal product categories
add_action( 'woocommerce_before_calculate_totals', 'cart_count_foil_seal_items',10,1);
function cart_count_foil_seal_items( $cart_object ) {
$seal_prod_tally = 0;
$foil_prod_tally = 0;
// Iterating through each item in cart
foreach ( $cart_object->get_cart() as $item_values ) {
// Get cart item data
$item_id = $item_values['data']->get_id(); // Product ID
$item_qty = $item_values['quantity']; // Item quantity
// Getting the object
$product = new WC_Product( $item_id );
$prod_cat = wp_get_post_terms($product->get_id(),'product_cat',array('fields'=>'slugs'));
//tally total
if (in_array('seal-stickers', $prod_cat)){
$seal_prod_tally += $item_qty;
}else if(in_array('foil-badges', $prod_cat)){
$foil_prod_tally += $item_qty;
}
}
foreach ( $cart_object->get_cart() as $item_values ) {
//Get cart item data
$item_id = $item_values['data']->get_id(); // Product ID
$item_qty = $item_values['quantity']; // Item quantity
// Getting the object
$product = new WC_Product( $item_id );
$prod_cat2 = wp_get_post_terms($product->get_id(),'product_cat',array('fields'=>'slugs'));
//apply discount to each item within category
if (in_array('seal-stickers',$prod_cat2)){
switch ($seal_prod_tally){
case 20000:
$item_values['data']->set_price(1327.01/20000);
if(!is_checkout()){
wc_add_notice(
sprintf( 'Quantity discount has been applied to item %s: 20,000 at $1327.01.', $item_values['data']->get_title()
), 'notice'
);
}
break;
case 30000:
$item_values['data']->set_price(1578.65/30000);
if(!is_checkout()){
wc_add_notice(
sprintf( 'Quantity discount has been applied to item %s: 30,000 at $1578.65.', $item_values['data']->get_title()
), 'notice'
);
}
break;
case 40000:
$item_values['data']->set_price(1853.05/40000);
if(!is_checkout()){
wc_add_notice(
sprintf( 'Quantity discount has been applied to item %s: 40,000 at $1853.05.', $item_values['data']->get_title()
), 'notice'
);
}
break;
case 50000:
$item_values['data']->set_price(2126.76/50000);
if(!is_checkout()){
wc_add_notice(
sprintf( 'Quantity discount has been applied to item %s: 50,000 at $2126.76.', $item_values['data']->get_title()
), 'notice'
);
}
break;
case 60000:
$item_values['data']->set_price(2405.98/60000);
if(!is_checkout()){
wc_add_notice(
sprintf( 'Quantity discount has been applied to item %s: 60,000 at $2405.98.', $item_values['data']->get_title()
), 'notice'
);
}
break;
default:
break;
}
}else if (in_array( 'foil-badges',$prod_cat2)){
switch ($foil_prod_tally){
case 25000:
$item_values['data']->set_price(5872.63/25000);
if(!is_checkout()){
wc_add_notice(
sprintf( 'Quantity discount has been applied to item %s: 25,000 at $5872.63.', $item_values['data']->get_title()
), 'notice'
);
}
break;
case 50000:
$item_values['data']->set_price(10815.47/50000);
if(!is_checkout()){
wc_add_notice(
sprintf( 'Quantity discount has been applied to item %s: 50,000 at $10815.47.', $item_values['data']->get_title()
), 'notice'
);
}
break;
default:
break;
}
}
}
}