单击Woocommerce中的更新购物车时自动重新计算数量

时间:2018-02-02 13:35:43

标签: php wordpress woocommerce cart product-quantity

因此,通过使用WooCommerce网站上的文档,我已经能够自动将新产品添加到购物篮中,效果很好。

我通过购买购物车中的当前数量并将其乘以百分比修饰符来计算产品数量,这也有效。

当我用新数量更新购物车时我的问题出现了,因为奖励产品数量没有得到更新。

add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 265;
        $found = false;

        // Get the current cart quantity
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $quantity = $cart_item['quantity'];
            $percentage = .25;
            $bonus = $quantity * $percentage;
        }

        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->id == $product_id )
                    $found = true;
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id, $bonus );
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id, $bonus );
        }
    }
}

有没有人知道如何在点击更新购物车时自动重新计算?

1 个答案:

答案 0 :(得分:0)

  

初步评论:

     
      
  • 当购物车为空时,您的代码无效,因为未定义$bonus变量并且您收到错误...

  •   
  • 此外,当您为数量设置变量浮点数时:因此,如果数量不是整数,则客户无法更改和更新购物车项目数量。

  •   

这有点复杂,您的奖金数量需要在以下情况下在另一个附加功能中更新:

  • 产品已添加到购物车
  • 购物车页面中的客户更新数量
  • 客户从购物车中删除商品

另外,像$_product->id这样的小错误在WC 3 +中应该$_product->get_id()

因此,您使用其他钩子函数重新访问的代码将是:

add_action( 'template_redirect', 'auto_add_product_to_cart', 50 );
function auto_add_product_to_cart() {
    if ( ! is_admin() ) {
        $cart = WC()->cart; // <== Cart object
        $product_id = 37; // <== Specific product to be auto added
        $bonus_rate = .25; // <== Bonus rate
        $found = false;

        // Check if product already in cart
        if ( ! $cart->is_empty() ) {
            $contents_count = $cart->get_cart_contents_count(); // The cart items count
            $bonus_qty = $contents_count * $bonus_rate; // Bonus quantity calculation
            // Loop through cart items
            foreach ( $cart->get_cart() as $cart_item ) {
                if ( $cart_item['data']->get_id() == $product_id ){
                    $found = true;
                    break; // Stop the loop
                }
            }
            // Product is not found in the loop, we add it
            if( ! $found )
                $cart->add_to_cart( $product_id, $bonus_qty );
        } else {
            // There is no items in cart, we add it
            $cart->add_to_cart( $product_id, $bonus_rate );
        }
    }
}

// Calculate, set and update bonus quantity
add_action( 'woocommerce_before_calculate_totals', 'conditional_bonus_quantity_calculation', 20, 1 );
function conditional_bonus_quantity_calculation( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $bonus_rate = .25; // <== Bonus rate
    $product_id = 37;  // <== Specific product to be auto added
    $contents_count = $cart->get_cart_contents_count(); // Total cart items count

    // Loop through cart items to check our specific product
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // If specific product is in cart
        if ( $cart_item['data']->get_id() == $product_id ){
            $calc_qty = $cart_item['quantity'] < 1 ? 1 : $cart_item['quantity'];
            // Bonus quantity calculation
            if( $contents_count > 1 )
                $bonus_qty = round( $contents_count - $calc_qty ) * $bonus_rate;
            else
                $bonus_qty = $bonus_rate;
            // Update item quantity
            $cart->set_quantity( $cart_item_key, $bonus_qty, false );
        }
    }
}

// Allowing float numbers quantity (step by .25) for a specific product
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 20, 2 );
function custom_quantity_input_args( $args, $product ) {
    // Only for your specific product ID on cart page
    if( $product->get_id() != 37 && is_cart() ) return $args;

    //$args['input_value'] = 0.25; // Default starting value (Optional)
    $args['min_value'] = 0.25;
    $args['step'] = 0.25;
    $args['pattern'] = '[0-9.]*';
    $args['inputmode'] = 'numeric';

    return $args;
}

// Allowing float numbers  in Stock management
remove_filter('woocommerce_stock_amount', 'intval');
add_filter('woocommerce_stock_amount', 'floatval');

代码进入活动子主题(或活动主题)的function.php文件。

经过测试和工作。