WooCommerce Product Bundles - Cart Quantity

后端 未结 3 714
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-24 23:28

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

3条回答
  •  死守一世寂寞
    2021-01-24 23:54

    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' );
    

提交回复
热议问题