WooCommerce 产品包 - 购物车数量 [英] WooCommerce Product Bundles - Cart Quantity

查看:39
本文介绍了WooCommerce 产品包 - 购物车数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

配置:

WordPress 4.1活动日历 PRO 3.9活动日历:WooCommerce 门票 3.9WooCommerce 2.2.11WooCommerce 产品包 4.6.2对于活动网站,销售以下门票:

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:

成人 $25儿童 $5婴儿 $0家庭 $55(最多包括 1-2 x 成人,1-3 儿童)家庭票配置为 WooCommerce 捆绑产品,捆绑产品为成人和儿童.成人的数量设置为 2,儿童的数量设置为 3

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

将家庭捆绑产品添加到购物车后,报告为购物车中的商品数量为 6.这由 1 x 家庭父产品和 2 x 成人和 3 x 儿童儿童产品组成.

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.

此处的预期结果应该是购物车报告 5 件商品 - 2 x 成人和 3 x 儿童.换句话说,从产品计数中忽略父产品.

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.

我的问题:需要什么才能让 WooCommerce 在计算购物车中的商品数量时忽略产品包的父产品?

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?

推荐答案

我相信按产品定价模式下的 Bundle 会自动计算捆绑商品的数量.在捆绑"模式下,项目数被假定为与父项相同.

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.

这个计数调整是在 Bundles 的购物车类中实现的...所以我认为它可以通过以下方式禁用:

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

我已经修改了上面的代码,因为看起来 Bundles 正在使用一个全局变量来访问插件的主类.另外,我认为 woocommerce_loaded 在主题加载之前触发,所以这不太可能奏效.我已更改为 init 钩子.

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.

编辑 2:捆绑包跳过对父项的计数,如果该项设置为使用每件产品的运费计算.但如果这不适用,那么您需要禁用 Bundles 过滤并应用您自己的:

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

这篇关于WooCommerce 产品包 - 购物车数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆