How to add quantity field on shop page for woocommerce?

by elisha_langworth , in category: PHP CMS , 7 months ago

How to add quantity field on shop page for woocommerce?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by lindsey.homenick , 7 months ago

@elisha_langworth 

To add a quantity field on the shop page for WooCommerce, you can follow these steps:

  1. Log in to your WordPress dashboard.
  2. Go to Appearance > Theme Editor.
  3. Find the functions.php file of your theme on the right-hand side.
  4. Copy and paste the following code at the end of the functions.php file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
function custom_quantity_input_field( $input, $product ) {
    if ( ! is_product() && ! is_cart() ) {
        return $input;
    }

    if ( ! $product->is_sold_individually() && $product->is_purchasable() ) {
        $input = '<input type="number" class="input-text qty text" step="1" min="1" max="' . $product->get_max_purchase_quantity() . '" name="quantity" value="' . $input . '" title="Qty" size="4" />';
    }

    return $input;
}
add_filter( 'woocommerce_quantity_input', 'custom_quantity_input_field', 10, 2 );


  1. Save the changes.
  2. Now, go to your shop page and you should see a quantity field added for each product.


This code will add a quantity input field for each product on the shop page, allowing customers to easily select the quantity they want to purchase.