0

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

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

4

1 回答 1

0

初步意见:

  • 当购物车为空时您的代码不起作用,因为$bonus未定义变量并且您收到错误...</p>

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

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

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

此外,在 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 文件中。

测试和工作。

于 2018-02-02T19:48:24.887 回答