WooCommerce Product Bundles – Cart Quantity

Configuration:

WordPress 4.1
The Events Calendar PRO 3.9
The Events Calendar: WooCommerce Tickets 3.9
WooCommerce 2.2.11
WooCommerce Product Bundles 4.6.2
For an Events website, selling the following tickets:

Read More

Adult $25
Child $5
Infant $0
Family $55 (Consists of up 1-2 x Adult, 1-3 Child)
The Family ticket is configured as a WooCommerce Bundled Product, with the bundled products being Adult and Child. Adult is set to a quantity of 2 and Child is set to a quantity of 3

When the Family bundled product is added to the cart, the number of items reported as being in the cart is 6. This is made up of the 1 x Family parent product and the 2 x Adult and 3 x Child children products.

The desired outcome here should be for the cart to report 5 items – 2 x Adult and 3 x Child. In other words, ignore the parent product from the product count.

My question: what is required in order to get WooCommerce to ignore the parent product of a Product Bundle when calculating the number of items in the cart?

Related posts

Leave a Reply

3 comments

  1. I believe that a Bundle in per-product pricing mode will automatically count the number of bundled items. When in “bundle” mode the number of items is assumed to be equal to the parent.

    This count tweak is achieved in the Bundles’ cart class… so I think it could be disabled via:

    function so_28359520_remove_bundles_counting(){
        global $woocommerce_bundles;
        remove_filter( 'woocommerce_cart_contents_count',  array( $woocommerce_bundles->display, 'woo_bundles_cart_contents_count' ) );
    }
    add_action( 'init', 'so_28359520_remove_bundles_counting' );
    

    Edit: I’ve revised the code above because it seems that Bundles is using a global variable to access the plugin’s main class. Additionally, I think that woocommerce_loaded fires before the theme is loaded, so that wasn’t likely to work ever. I’ve changed to the init hook.

    Edit 2: Bundles will skip counting on the parent item if that item is set to use per-product shipping calculations. But if that isn’t applicable then you need to disable the Bundles filtering and apply your own:

    function so_28359520_cart_contents_count( $count ) {
    
        $cart = WC()->cart->get_cart();
    
        $subtract = 0;
    
        foreach ( $cart as $key => $value ) {
    
            if ( isset( $value[ 'stamp' ] ) && ! isset( $value[ 'bundled_by' ] ) ) {
                $subtract += $value[ 'quantity' ];
            }
        }
    
        return $count - $subtract;
    
    }
    add_filter( 'woocommerce_cart_contents_count',  'so_28359520_cart_contents_count' );
    
  2. With heaps of help from @helgatheviking I have been able to come up with the following solution in my functions.php:

    function cit_update_cart_count() {
        global $woocommerce;
        $count = 0;
        $cart = $woocommerce->cart->get_cart();
        foreach ($cart as $key => $value) {
            if (!isset($value['bundled_items'])) {
                $count += $value['quantity'];
            }
        }
        $woocommerce->cart->cart_contents_count = $count;
    }
    add_action('init','cit_update_cart_count',10);
    
  3. You can use the same function in early posts that is written here

      function so_28359520_cart_contents_count( $count ) {
    
        $cart = WC()->cart->get_cart();
    
        $subtract = 0;
    
        foreach ( $cart as $key => $value ) {
    
            if ( isset( $value[ 'stamp' ] ) && ! isset( $value[ 'bundled_by' ] ) ) {
                $subtract += $value[ 'quantity' ];
            }
        }
    
        return $count - $subtract;
    
    }
    

    But you must use it inside class-wc-pb-cart.php

    and inside construct add
    add_filter( 'woocommerce_cart_contents_count', 'so_28359520_cart_contents_count' );

    That works like a charm , but in my theme I had problem with WC()->cart->cart_contents_count in header.php , somehow the filter is not applied on that , but using

    <?php 
        global $woocommerce;
        // get cart quantity
        $qty = $woocommerce->cart->get_cart_contents_count();
    ?>
    

    Inside header.php the filter is applied and I get the correct count.