Woocommerce cart notice showing multiple times

2019-06-02 05:07发布

I have a function that's working properly except for it displays the notice twice in the cart instead of once. The function applies a discount and also shows a notice. The function looks for items of a particular category adds the total and if it meets the discount amount it applies the discount and shows a notice. Right now, even if there's only one item in the cart, it's showing the notice twice.

I've tried adding wc_clear_notices() but that will clear other notices I need like min max notices for minimum order amounts on categories. If I add in wc_clear_notices() at the beginning of the function or in any of the foreach statements it will clear the other min/max notice before it even shows.

Here are some questions I've already looked at but just say to use wc_clear_notices() which isn't working because other notices get cleared:

WooCommerce Notice, run only once

Show wc_add_notice only one time

Here's the code I currently have that correctly discounts items and displays notices but displays the notices twice instead of once:

//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;
        }            
    }      
 }

}

Screenshot of Duplicated Notice in Cart

1条回答
Explosion°爆炸
2楼-- · 2019-06-02 05:41

The multiple notices were caused when the customer updated the cart with the Update Cart button so a quick fix was simply clearing notices every time the customer clicked the Update Cart button.

It would be better to solve the problem at its source which would be finding a different way to display notices without duplication but until then this gets the desired effect.

//clear notices on cart update
function clear_notices_on_cart_update() { 
    wc_clear_notices();
}; 

// add the filter 
add_filter( 'woocommerce_update_cart_action_cart_updated', 'clear_notices_on_cart_update', 10, 1 ); 
查看更多
登录 后发表回答