简体   繁体   中英

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. 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.

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.

Now in your provided code, you are complicating things without any need to use a foreach loop. Your code can be simplified in a more efficient way using in_array() function as follows:

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). Tested and works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM