删除购物车中的捆绑商品数量woocommerce

时间:2015-07-13 14:19:24

标签: wordpress count woocommerce bundle cart

我使用wordpress 4.2.2和woocommerce 2.3.11构建我的电子商务销售葡萄酒 我买了#34;产品包" (v.4.9.5)woocommerce插件。

我已经在我的function.php文件中插入了这个函数

add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
    $multiples = 6;
    $total_products = 0;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $total_products += $values['quantity'];
    }
    if ( ( $total_products % $multiples ) > 0 )
        wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ), 'error' );
}

因为我需要以6.的倍数出售瓶子 现在我对捆绑的产品有这个问题,因为在购物车中也会计算父项目 如果我创建一个装有6瓶的捆绑包,在购物车中我会收到消息"您需要购买大量%s产品"因为总数是7(6瓶加1捆) 我写了对woothemes的支持,我回答了这个问题:

  

默认情况下,不计算Bundle内容。捆绑将被计算在内   作为一个项目,无论它包含多少产品。

     

使用产品包并需要计算内容时,您需要   使用WC() - > cart-> get_cart_contents_count()方法计算所有   购物车项目。

------更新-------

我尝试在我的函数中添加答案中的代码,所以现在我的function.php看起来像这样:

<?php
/**
 * Child-Theme functions and definitions
 */

 // check that cart items quantities totals are in multiples of 6
add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
    $multiples = 6;
    $total_products = 0;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        $total_products += $values['quantity'];
    }
    if ( ( $total_products % $multiples ) > 0 )
        wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ), 'error' );
}

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


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

?>

但没有任何事情发生。我还尝试一个新的wordpress和woocommerce安装与默认主题(二十三和十二十五)进行测试,如果在主题中有一些javascript干扰和改变数量,但结果是相同的。 如果我有1瓶6瓶装,我会收到第一个功能的消息。

2 个答案:

答案 0 :(得分:1)

我已经回答了这个here。您需要禁用Bundle的功能并编写自己的功能以跳过对包容器的计数。

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


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

有一点需要注意的是,Javascript可能会更新菜单/小部件中的计数,而不是出于某种原因考虑到这一点。但是,当我&#34;查看来源&#34;我得到了所需的数量(即:一捆6瓶显示6个)。

或者作为替代方法,您可以修改计数功能以不计算捆绑容器。未测试的:

add_action( 'woocommerce_check_cart_items', 'woocommerce_check_cart_quantities' );
function woocommerce_check_cart_quantities() {
    $multiples = 6;
    $total_products = 0;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
        if ( isset( $value[ 'stamp' ] ) && ! isset( $value[ 'bundled_by' ] ) ) {
            continue; // skip the bundle container
        } else {
            $total_products += $values['quantity'];
        }
    }
    if ( ( $total_products % $multiples ) > 0 )
        wc_add_notice( sprintf( __('You need to buy in quantities of %s products', 'woocommerce'), $multiples ), 'error' );
}

答案 1 :(得分:0)

我遇到了这个问题,直到我意识到我需要使用WC() - &gt; cart-&gt; get_cart_contents_count()而不是WC() - &gt; cart-&gt; cart_contents_count

以为我会把它放在这里以防万一其他人正在拔毛......