为 WooCommerce 产品启用十进制数量和库存 [英] Enable decimal quantities and stock for WooCommerce products

查看:37
本文介绍了为 WooCommerce 产品启用十进制数量和库存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将产品的默认数量从 1 更改为 0,1,但我似乎无法弄清楚.

I want to change the default quantity from the products, from 1 to 0,1 but I can't seem to figure it out.

我尝试了以下方法:

function custom_quantity_input_args($args, $product) {
    $args['input_value'] = 0.10;
    $args['min_value'] = 0.10;
    $args['step'] = 0.10;
    $args['pattern'] = '[0-9.]*';
    $args['inputmode'] = 'numeric';

    return $args;
}

这样做的问题是修改了从购物车输入的数量,这不是我想要的.

The problem with this is that modifies the quantity input from cart as well, which isn't what I want.

更具体地说,我想要以下内容:

To be more specific I want the following:

  1. 当我进入产品页面时,我想显示 0,1;
  2. 当我进入购物车页面时,我想显示当前数量;

我上面提到的解决方案在产品页面和购物车页面中都显示了 0,1.
我找到了另一个解决方案,但它同时显示了产品和购物车中的当前数量,这又不是我想要的.

The solution I mention above shows 0,1 both in product page and in cart page.
I found another solution, but it shows the current quantity both in product and in cart which, again, it's not what I want.

有什么想法吗?

推荐答案

基于 WooCommerce 中特定产品类别的十进制数量步骤 答案代码,请尝试以下重新访问的代码:

Based on Decimal quantity step for specific product categories in WooCommerce answer code, try the following revisited code:

// Defined quantity arguments
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 9000, 2 );
function custom_quantity_input_args( $args, $product ) {
    if( ! is_cart() ) {
        $args['input_value'] = 0.1; // Starting value
    }
    $args['min_value']   = 0.1; // Minimum value
    $args['step']        = 0.1; // Quantity steps
    
    return $args;
}

// For Ajax add to cart button (define the min value)
add_filter( 'woocommerce_loop_add_to_cart_args', 'custom_loop_add_to_cart_quantity_arg', 10, 2 );
function custom_loop_add_to_cart_quantity_arg( $args, $product ) {
    $args['quantity'] = 0.1; // Min value

    return $args;
}

// For product variations (define the min value)
add_filter( 'woocommerce_available_variation', 'filter_wc_available_variation_price_html', 10, 3);
function filter_wc_available_variation_price_html( $data, $product, $variation ) {
    $data['min_qty'] = 0.1;
        
    return $data;
}

// Enable decimal quantities (in frontend and backend)
remove_filter('woocommerce_stock_amount', 'intval');
add_filter('woocommerce_stock_amount', 'floatval');

代码位于活动子主题(或活动主题)的functions.php 文件中.经测试有效.

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

这篇关于为 WooCommerce 产品启用十进制数量和库存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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