WooCommerce 自定义数量输入参数基于自定义字段

WooCommerce custom quantity input args based on custom field

提问人:Kuiio 提问时间:11/10/2023 最后编辑:LoicTheAztecKuiio 更新时间:11/11/2023 访问量:43

问:

我已经为简单产品设置了自定义数量输入,它按预期工作,但它在可变产品上中断,所以我想让它在可变产品的每个变体上工作,但我无法弄清楚如何

简单的产品代码:

add_filter( 'woocommerce_quantity_input_args', 'productQuantityInputArgs', 10, 2);
function productQuantityInputArgs( $args, $product ) {
    if ( ! is_cart() ) {
        $product = wc_get_product();
        $customPasirinkimoKiekis = $product->get_meta('_quantity_product_in_package');
        $stock = $product->get_meta('_stock_quantity');
        $productType = $product->get_type();
        if($productType === 'simple') {
            $args['max_value'] = $stock - ($stock % $customPasirinkimoKiekis);
            $args['input_value'] = $customPasirinkimoKiekis;
            $args['min_value'] = $customPasirinkimoKiekis;
            $args['step'] = $customPasirinkimoKiekis;
        }
    }
    return $args;
}

删除 if type === 'simple' 会破坏可变产品页面,但产品的每个变体都具有与简单产品相同的所需元数据,我不知道如何为产品的可变/变体执行此操作

php wordpress woocommerce 库存 产品数量

评论

0赞 LoicTheAztec 11/14/2023
下面有一些问题的答案,请等待一些反馈。

答:

0赞 Vasim Shaikh 11/11/2023 #1

在WooCommerce中修改可变产品的数量输入时,请始终认为可变产品的每个变体都可以有自己的设置。

请使用以下过滤器代码检查可变产品的工作情况:

add_filter('woocommerce_quantity_input_args', 'custom_product_quantity_input_args_variable', 10, 2);

function custom_product_quantity_input_args_variable($args, $product) {
    if (!is_cart() && $product->is_type('variable')) {
        global $product;

        // Get custom meta fields
        $custom_pasirinkimo_kiekis = $product->get_meta('_quantity_product_in_package');
        
        $args['min_value'] = $custom_pasirinkimo_kiekis;
        $args['step'] = $custom_pasirinkimo_kiekis;

        // Set max value based on stock of selected variation
        $max_value = 0;

        foreach ($product->get_children() as $variation_id) {
            $variation = wc_get_product($variation_id);

            // Check if variation is in stock
            if ($variation->is_in_stock()) {
                $stock = $variation->get_stock_quantity();
                $max_value = max($max_value, $stock - ($stock % $custom_pasirinkimo_kiekis));
            }
        }

        $args['max_value'] = $max_value;
    }

    return $args;
}
1赞 LoicTheAztec 11/11/2023 #2

若要基于更改产品数量输入参数的自定义字段处理所有产品类型,您需要更完整的内容。

将所有相关代码替换为以下内容:

// For ajax add to cart
add_filter( 'woocommerce_loop_add_to_cart_args', 'loop_ajax_add_to_cart_quantity_fix', 10, 2 );
function loop_ajax_add_to_cart_quantity_fix( $args, $product ) {
    if ( $custom_qty = $product->get_meta('_quantity_product_in_package') ) {
        $args['quantity'] = $custom_qty;
    }
    return $args;
}

// For Single products and Cart pages
add_filter( 'woocommerce_quantity_input_args', 'filter_product_quantity_input_args', 10, 2);
function filter_product_quantity_input_args( $args, $product ) {
    if ( $custom_qty = $product->get_meta('_quantity_product_in_package') ) {
        $stock_qty = $product->get_stock_quantity();
        
        if ( ! is_cart() ) {
            $args['input_value'] = $custom_qty;
        }
        $args['min_value']   = $custom_qty;
        $args['max_value']   = $stock_qty - ($stock_qty % $custom_qty);
        $args['step']        = $custom_qty;
    }
    return $args;
}

// Variable products (Selected product variation)
add_filter( 'woocommerce_available_variation', 'filter_selected_variation_quantity_input_args', 10, 3 );
function filter_selected_variation_quantity_input_args( $data, $product, $variation ) { 
    if ( $custom_qty = $product->get_meta('_quantity_product_in_package') ) {
        $stock_qty = $variation->get_stock_quantity();

        $data['min_qty'] = $custom_qty;
        $data['max_qty'] = $stock_qty - ($stock_qty % $custom_qty);
    }
    return $data;
}

代码位于子主题的函数.php文件中(或插件中)。经过测试并有效。