简体   繁体   English

自定义数量字段在 Woocommerce 存档页面上不起作用

[英]Custom quantity field doesn't work on Woocommerce archive pages

I have added a 'quantity' field and 'Add to cart' button to the Shop page and the Category pages for each product using custom code and have also set the min qty, max qty and step for the quantity field based on product IDs.我已经使用自定义代码为每个产品的商店页面和类别页面添加了一个“数量”字段和“添加到购物车”按钮,并且还根据产品 ID 为数量字段设置了最小数量、最大数量和步长。 These products are wine and for some of them you can purchase a minimum of 6 with the step being 6 eg 6, 12, 18, 24 etc and for some you can purchase a minimum of 12 with the step being 12 eg 12, 24, 36, 48 etc.这些产品是葡萄酒,对于其中一些产品,您可以购买至少 6 个,步骤为 6,例如 6、12、18、24 等;对于某些产品,您可以购买至少 12 个,步骤为 12,例如 12、24, 36、48 等

This all displays fine but the quantity and price don't carry across to the cart page.这一切都显示正常,但数量和价格不会传递到购物车页面。 The qty always sets itself to the minimum regardless of how many are ordered and the price only displays for one unit on the cart page when a product is ordered from the Shop or Category page.无论订购了多少数量,数量始终设置为最小值,并且当从“商店”或“类别”页面订购产品时,购物车页面上仅显示一个单位的价格。

function wpse_quantity_input_default( $args, $product ) {

    $productID = $product->id;

    foreach( WC()->cart->get_cart() as $key => $item ){

            // MINIMUM / MAXIMUM
            if($productID == '566' || $productID == '562' || $productID == '1177' || $productID == '1181' || $productID == '1183' || $productID == '1185' || $productID == '1242' || $productID == '1250' || $productID == '1251' || $productID == '1252' || $productID == '1254') {

                    $args['input_value'] = 12;
                    $args['max_value']  = 240;  // Maximum value (variations)
                    $args['min_value']  = 12;   // Minimum value (variations)
                    $args['step']       = 12;    // Quantity steps

            } else {

                    $args['input_value'] = 6;
                    $args['max_value']  = 120;  // Maximum value (variations)
                    $args['min_value']  = 6;   // Minimum value (variations)
                    $args['step']       = 6;    // Quantity steps

            }

    } 

return $args;

}

add_filter( 'woocommerce_quantity_input_args', 'wpse_quantity_input_default', 1000, 2 );

The described problem comes from Ajax add to cart button (and its quantity field), so you should provide in your question all related code.所描述的问题来自 Ajax 添加到购物车按钮(及其数量字段),因此您应该在问题中提供所有相关代码。

Now in your provided code, you are complicating things without any need to use a foreach loop.现在,在您提供的代码中,您无需使用 foreach 循环即可使事情复杂化。 Your code can be simplified in a more efficient way using in_array() function as follows:可以使用 in_array() 函数以更有效的方式简化您的代码,如下所示:

add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 1000, 2 );
function custom_quantity_input_args( $args, $product ) {
    $products_ids = array(562, 566, 1177, 1181, 1183, 1185, 1242, 1250, 1251, 1252, 1254);
    
    if( in_array( $product->get_id(), $products_ids ) ) {
        $args['input_value'] = 12;
        $args['max_value']  = 240;  // Maximum value (variations)
        $args['min_value']  = 12;   // Minimum value (variations)
        $args['step']       = 12;    // Quantity steps
    } else {
        $args['input_value'] = 6;
        $args['max_value']  = 120;  // Maximum value (variations)
        $args['min_value']  = 6;   // Minimum value (variations)
        $args['step']       = 6;    // Quantity steps
    }
    return $args;
}

Code goes in functions.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce存档页面上的自定义数量字段功能问题 - Custom quantity field functionality issue on WooCommerce archive pages 在 WooCommerce 存档页面上获取并显示产品自定义字段 - Get and display product custom field on WooCommerce archive pages 用ACF自定义字段替换Woocommerce分类标准存档页面标题 - Replace Woocommerce taxonomy archive pages title with ACF custom field 在 Woocommerce 中创建自定义数量字段 - Create a custom quantity field in Woocommerce WooCommerce Ajax 添加到购物车数量不起作用 - WooCommerce Ajax add to cart quantity doesn't work woocommerce_quantity_input在添加到购物车循环中不起作用 - woocommerce_quantity_input doesn't work in add to cart loop 自定义帖子类型存档,更多页面无效 - Custom post type archive, more pages don't work 在 WooCommerce 存档页面上显示产品名称和星级之间的自定义字段 - Show custom field between product name and star ratings on WooCommerce archive pages “自定义存档”页面不适用于日期或标签,但适用于类别 - Custom Archive page doesn't work with dates or tags but works with categories Woocommerce单个产品页面上的自定义正负数量按钮 - Custom plus and minus quantity buttons in on Woocommerce single product pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM