我有一個正常工作的功能,除了在購物車中顯示兩次通知而不是一次。該功能應用折扣並顯示通知。該函數查找特定類別的項目會增加總額,如果它符合折扣金額,則應用折扣並顯示通知。現在,即使購物車中只有一件商品,它也會顯示兩次通知。Woocommerce購物車提示多次顯示
我試過添加wc_clear_notices()
,但是會清除我需要的其他通知,例如類別上最小訂單金額的最小最大通知。如果我在函數的開始處或任何foreach語句中添加wc_clear_notices()
,它將在其顯示前清除其他最小/最大通知。
這裏有一些問題我已經看過,但只是說使用wc_clear_notices()
這是行不通的,因爲其他通知被清除:
WooCommerce Notice, run only once
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;
}
}
}
}
感謝您澄清。它看起來像它添加費用顯示的通知實際上低於購物車的運費,這不會起作用,特別是如果我有多個通知。 :/ [計算費用通知的屏幕截圖](https://i.gyazo.com/8300cc90915c83ca3ae73723b63e3b3c.png) – Mark
看起來我需要檢查在此函數運行後是否應用了折扣,然後顯示通知?你有什麼想法嗎?也許一個會話變量綁在購物車中? – Mark
如果我不清楚,我很抱歉。我沒有使用你的代碼示例。 add_fee()添加低於運費的折扣,這是不起作用的。我在問,除了add_fee()例子之外,你是否還有其他選擇。 – Mark