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

Automatically recalculate quantity when clicking on update cart in Woocommerce

因此,通过使用 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 );
        }
    }
}

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

Preliminary remarks:

  • Your code doesn't work when cart is empty as $bonus variable is not defined and you get an error…

  • Also as you are setting a variable float number for quantity: So if the quantity is not an integer, customer can't change and update the cart items quantities.

这有点复杂,您的 奖金数量 需要在另一个挂钩函数中更新:

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

此外,在 WC 3+

中应该有 $_product->id 之类的小错误,应该改为 $_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 文件。

已测试并有效。