设置最小输入量,甚至在Woocommerce中添加到购物车的ajax上 [英] Set minimum input quantity even on ajax add to cart in Woocommerce

查看:65
本文介绍了设置最小输入量,甚至在Woocommerce中添加到购物车的ajax上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的function.php上添加了以下代码(每个产品最少6个数量),这反映在我的产品类别页面上……

I have added the below code on my function.php (minimum as 6 qty for every product) it is reflect on my product category page…

add_filter( 'woocommerce_quantity_input_min','woocommerce_quantity_input_min_callback', 10, 2 );
function woocommerce_quantity_input_min_callback( $min, $product ) {
    $min = 6;  
    return $min;
}

但是在单击添加到购物车"按钮(Ajax)时,购物车页面上只有1个数量,而不是6个数量.如何解决此问题?

But while clicking the add to cart button (Ajax) the cart page only have 1 qty not 6 qty… How to fix this?

每种产品在购物车页面上需要数量6.

Need qty 6 on cart page for each product.

推荐答案

您还需要设置添加到购物车事件(+ Ajax)的数量,并设置输入值+最小输入同时值数量.因此,请尝试以下操作:

You need also to set the quantity on add to cart event (+ Ajax) and to set the input value + the minimum input value quantity at the same time. So try the following instead:

// Set product quantity added to cart (handling ajax add to cart)
add_filter( 'woocommerce_add_to_cart_quantity','woocommerce_add_to_cart_quantity_callback', 10, 2 );
function woocommerce_add_to_cart_quantity_callback( $quantity, $product_id ) {
    if( $quantity < 6 ) {
        $quantity = 6;
    }
    return $quantity;
}

// Set the product quantity min value
add_filter( 'woocommerce_quantity_input_args', 'woocommerce_quantity_input_args_callback', 10, 2 );
function woocommerce_quantity_input_args_callback( $args, $product ) {
    $args['input_value'] = 6;
    $args['min_value']   = 6;

    return $args;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于设置最小输入量,甚至在Woocommerce中添加到购物车的ajax上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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