Magnus Pilegaard
Magnus Pilegaard

Reputation: 393

Don't include product in WooCommerce cart count

There is a product which I don't want to be included in the mini-cart items count.

This is the code that I use to output this mini-cart count:

        <span class="cart-quantity">
        <?php echo sprintf(_n('%d', '%d', $woocommerce->cart->cart_contents_count, 'organica'), $woocommerce->cart->cart_contents_count);?>
    </span>

How can I do to don't include a specific product ID in this mini-cart item count?

Thanks

Upvotes: 2

Views: 324

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254493

Here is the way to achieve this cart count without counting items related to a specific product ID, that you will have to set in the commented code below:

<?php

    // HERE set the product ID that hasn't to be counted
    $product_not = 28;

    // Initializing the count
    $cart_items_count = 0;

    // Iterating through each items in cart
    foreach(WC()->cart->get_cart() as $cart_item){
        if($cart_item['product_id'] != $product_not)
            $cart_items_count++;
    }

?>

    <span class="cart-quantity">
        <?php echo sprintf(_n('%d', '%d', $cart_items_count, 'organica'), $cart_items_count);?>
    </span>

Upvotes: 1

Related Questions