我希望数量 Select 器值根据我们在Shipping>Product Weight for Simple Products中设置的权重进行更改. 根据下图,当我们将产品的重量设置为0.5千克时,产品数量 Select 器从0.5开始,如果我们将其设置为1千克,则产品数量 Select 器从1开始.最后,当我们 for each 数字设置权重时,数量 Select 器应该基于我们定义的重量数字来启动. 我修改了一个代码,但它不适用于小于1的值.

enter image description here

/*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 );

推荐答案

要使用的正确代码替换为:

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');

代码放在活动子主题(或活动主题)的函数.php文件中.经过测试,效果良好.


当加载一个权重为0.5的.product的页面时:

enter image description here

在产品上设置正确的数量输入,并以0.5的步长递增

在购物车页面上,一切都像预期的那样工作,步长为0.5.

Php相关问答推荐

谷歌云SQL / mysql谁是对的?

PHP php_mongodb.dll for 32bit

在WooCommerce产品档案页面中显示产品销售百分比

如何使用`preg_replace`删除重复的空格字符

在WooCommerce中禁用客户用户角色的纳税计算

WooCommerce在购买后多次重定向

Symfony 6.2 IsGraned具有多个角色

如何从该字符串中删除这些图标转换的ASCII问号字符?

如何使用barryvdh/laravel-Snappy在两个打印页面上将表格分成两部分?

Shopware 6插件:由于删除了配置密钥,更新后配置值不正确

Woocommerce API - 图像问题

Symfony:从控制器内部调用自定义命令

使用 phpseclib 验证 RSA-PSS

通过ajax发送数据到下拉列表未完全设置为所选

如何为自定义帖子类型的自定义角色提供功能

获取 Shopware 6 集合元素的值,PHP

如何将文件上传添加到 WooCommerce 结帐?

PHP Symfony 在测试.env文件中将接口自动装配作为构造函数参数,但使用接口的特定类

如何在光速网络服务器中使用 apache ..htaccess

Google Drive - 仅使用 FileID 获取文件的 FileSize