根据重量在WooCommerce中更改简单产品的输入数量步进值

huangapple go评论67阅读模式
英文:

Change input quantity step value for simple product based on weight in WooCommerce

问题

我希望数量选择器的值根据我们在“运费 > 产品重量”中设置的重量而改变,适用于普通产品。

根据下面的图片,当我们将产品的重量设置为0.5 kg时,产品数量选择器从0.5开始,如果我们将其设置为1kg,则从1开始。最后,当我们将重量设置为每个数字时,数量选择器应该基于我们定义的重量数字开始。

我已经修改了一段代码,但对小于一的值无效。

英文:

I want the quantity selector value to change based on the weight we set in Shipping > Product Weight for simple products.
According to the image below, when we set the weight of the product to 0.5 kg, the product quantity selector starts from 0.5, and if we set it to 1kg, it starts from 1. Finally when we set the weight to each number the quantity selector should be start based on weight number that we have defined.
I have modified a code but it doesn't work for values less than one.

根据重量在WooCommerce中更改简单产品的输入数量步进值

/*Quantity Selector Based On Simple*/
function custom_quantity_selector_min_value( $min, $product ) {
    $weight = $product->get_weight();
    if ( $weight > 0 ) {
        $min = $weight;
    }
    return $min;
}

add_filter( 'woocommerce_quantity_input_min', 'custom_quantity_selector_min_value', 10, 2 );

//Modify the quantity selector step value.

function custom_quantity_selector_step( $step, $product ) {
    $weight = $product->get_weight();
    if ( $weight > 0 ) {
        $step = $weight;
    }
    return $step;
}

add_filter( 'woocommerce_quantity_input_step', 'custom_quantity_selector_step', 10, 2 );

//Update the quantity selector value dynamically.

function custom_quantity_selector_value( $input_value, $product ) {
    $weight = $product->get_weight();
    if ( $weight > 0 ) {
        $input_value = $weight;
    }
    return $input_value;
}

add_filter( 'woocommerce_quantity_input_value', 'custom_quantity_selector_value', 10, 2 );

答案1

得分: 3

适用于(已更新)的正确代码替换:

  • 简单产品,
  • 或可变产品(及其变体)。

它将在以下情况下正常运行:

  • 单个产品页面,
  • 购物车页面。
add_filter( 'woocommerce_quantity_input_args', 'cart_variation_quantity_input_args', 10, 2 );
function cart_variation_quantity_input_args( $args, $product ){
    $product_weight = $product->get_weight();
    
    if( $product_weight > 0 ) {
        if ( ! is_cart()) {
            $args['input_value'] = $product_weight;
        } 
        $args['step'] = $args['min_value'] = $product_weight;
    }
    return $args;
}

确保您也已添加(用于库存管理):

remove_filter('woocommerce_stock_amount', 'intval');
add_filter('woocommerce_stock_amount', 'floatval');

代码放在您活动的子主题(或活动主题)的functions.php文件中。经过测试,可以正常运行。

加载带有重量为0.5的产品的页面时:

根据重量在WooCommerce中更改简单产品的输入数量步进值

正确的数量输入已设置在产品上,并且以0.5的步长增加(也以1的常规步长增加)。

在购物车页面上,一切都按预期运行,步长为0.5(也以1的常规步长增加)。

相关链接(有关变体):https://stackoverflow.com/questions/76597006/change-input-quantity-step-value-based-on-selected-variation-weight-in-woocommer/76599597#76599597

英文:

The correct code replacement to be used for (Updated):

  • simple products ,
  • or variable products (and their variations).

It will work smoothly as expected in:

  • single product pages,
  • and cart page too.
add_filter( 'woocommerce_quantity_input_args', 'cart_variation_quantity_input_args', 10, 2 );
function cart_variation_quantity_input_args( $args, $product ){
    $product_weight = $product->get_weight();
    
    if( $product_weight > 0 ) {
        if ( ! is_cart()) {
            $args['input_value'] = $product_weight;
        } 
        $args['step'] = $args['min_value'] = $product_weight;
    }
    return $args;
}

Be sure that you have added too (for stock management):

remove_filter('woocommerce_stock_amount', 'intval');
add_filter('woocommerce_stock_amount', 'floatval');

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


When loading a page with a product that has 0.5 as weight:

根据重量在WooCommerce中更改简单产品的输入数量步进值

The correct quantity input is set on the product, and it increases with a step of 0.5 (and with a normal step of 1 too).

On cart page, everything works as expected, with a step of 0.5 (and with a normal step of 1 too).

Related (for variations): https://stackoverflow.com/questions/76597006/change-input-quantity-step-value-based-on-selected-variation-weight-in-woocommer/76599597#76599597

huangapple
  • 本文由 发表于 2023年7月3日 05:41:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76600906.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定