WooCommerce: Add “Quantity” Label in front of Add to Cart Button

Here’s another little WooCommerce tweak for your website user experience: let’s add some text in front of the Add to Cart button that says “Quantity: “.

As usual, we go looking for the right “hook”… the WooCommerce plugin is full of them and adding some custom content in the exact position we want is very easy. Enjoy!

Add “quantity” label before Add to Cart @ Single Product Page

PHP Snippet: Add “Quantity” Label in front of Add to Cart Button – WooCommerce

/**
 * @snippet       Add "Quantity:" before Add to Cart Button - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_before_add_to_cart_quantity', 'bbloomer_echo_qty_front_add_cart' );

function bbloomer_echo_qty_front_add_cart() {
	global $product;
	if ( $product->get_min_purchase_quantity() == $product->get_max_purchase_quantity() ) return;
 	echo '<div class="qty">Quantity: </div>'; 
}

And a couple of CSS lines as well:

div.qty {
    float: left;
    padding: 10px;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Custom Add to Cart URLs – The Ultimate Guide
    In WooCommerce you can add a product to the cart via a custom link. You just need to use the “add-to-cart” URL parameter followed by the product ID. This tutorial will show you how to create custom URLs to add simple, variable and grouped products to the cart – as well as defining the add […]
  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

53 thoughts on “WooCommerce: Add “Quantity” Label in front of Add to Cart Button

  1. Annoyingly it isn’t quite as simple as this. When a product has a stock level of 1 WooCommerce hides the quantity input. this means you are left with your pseudo ‘Quantity’ label showing and no corresponding input. Also worth noting WooCommerce outputs a visually hidden label element for the input, therefore it would be a good idea to add a aria-hidden=”true” attribute to the added element so assistive technology isn’t getting duplicate content.

    1. Thank you. I’ve fixed the “sold individually” scenario, so the label doesn’t appear now when there is no quantity input. Thanks re: the aria-hidden tip as well!

  2. how can i use this code so it uses the translations of Quantity and not the english word. Thanks in advance.

    add_action( 'woocommerce_before_add_to_cart_quantity', 'wp_echo_qty_front_add_cart' );
    function wp_echo_qty_front_add_cart() {
     echo '<div class="qty">Quantity:</div>'; 
    }
    1. In order to make a string translatable you have to wrap it in a __() function: https://codex.wordpress.org/I18n_for_WordPress_Developers#Translatable_strings – hope this helps πŸ™‚

  3. Hello how i can do to add quantity in just one product? thanks!

    1. I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy πŸ™‚

  4. HI Rodolfo,

    This is an incredibly clear and helpful little post and snippet(s) – thanks so much.

    And it came up close to the top of a google search for ‘woocommerce product quantity field label’

    I am using product variations for simple venue hire products for a church hall. So i’ve used the variatons to set the number of hours in the hire booking, and the quantity field to set the number of days the hire is repeated. So it was really important to be able to label that field – i’ve done so using ‘days’ instead of ‘quantity.

    1. Great!

  5. Greetings,
    would you help me to customize woocommerce product page

    thank you in advance & have a wonderful day.

    1. Hey Jaouahar, thanks so much for your comment! If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. I’ve added this code, but the only thing that doesn’t work is the plus or minus buttons which we’ve added to the sides of the quantity box, using a previous code of yours.

    The quantity box stays empty and the plus or minus buttons won’t work.

    Without your filter for the quantity div, the plus or minus buttons work ok.

    Not sure what would be the best way to resolve this?

    Kind Regards,

    Andrew

    1. I guess some adjustment might be needed to make them work together, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  7. Normally quantity was in front of the add to cart button. Few plugins installed after checking my website i see its above of add to cart button. I tried code here but it didnt work.

    1. Here photo
      https://ibb.co/JpK3vZP

      Thanks you

      1. Hi Gary, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet?

        To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentyseventeen” theme (load the snippet there in functions.php) – does it work? If yes, you have a problem with your current theme or one of the plugins.

        Hope this helps!

        R

  8. Hello, the snippet isn’t working anymore, it doesn’t apply the amount in the shopping cart. Can you please check this?

    1. Hey Mo, tested on Woo 3.5.1, works perfect. Thank you πŸ™‚

  9. is it possible to add this texte but ONLY for some products ?

    1. Hello Julienne, thanks for your comment! I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know πŸ™‚

  10. As of 3.0 you should use: woocommerce_before_add_to_cart_quantity

    1. That’s awesome Lucas. Thank you so much πŸ™‚

  11. it didnt work well for me πŸ™

    It got mixed up with some other info

    Can you please help me to fix?
    Thanks in advance

    1. pls i am using divi 2.2 theme i forgot to add

    2. got it fixed by tweaking cs πŸ˜‰ thanks buddy God bless

  12. This code works best for me:

    .quantity:before {
    content:”Quantity:”;
    margin-right:10px
    }
    .quantity input {
    display:inline;
    margin-right:30px
    }
    .woocommerce-variation-add-to-cart {
    margin-top:20px
    }

    1. Thanks Chris πŸ™‚

  13. It worked like a charm!

  14. Hi Rodolfo,

    I managed to change the label but after adding the CSS code in my X theme, I am still not able to see the lable on the left side of the box. It’s still on top of the quantity box.

    Can you please email me, as I have a few projects to discuss as well.

    Cheers

    1. Hey Chirag, thanks so much for your comment! I guess this is X-theme specific, so the CSS will need to be changed in order to work. If you’d like to get a quote, feel free to contact me here. ~R

  15. Hello Rodolfo, Thank you so much for the explanation !
    it works great for me.
    I would need a little thing additional to have it perfect for my need : how to add a label (for quantity unit) AFTER the quantity input field?
    Thank you in advance for your help !
    Best regards,
    Julie

    1. Hey Julie, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  16. how to add any text after add to cart button in the loop/shop page (not single view)?

    1. Hey Gen, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  17. Hi this is great, but how can I remove this display for products that have no quantity box . So I want that the label will be conditionally displayed. Thanks

    1. You could override the default Woo template called

      global/quantity-input.php

      That way the quantity label will only show when the quantity input field is displayed.

      1. Gantsta, thanks so much for your comment! However, it’s not a great idea overriding templates, just so you know πŸ™‚

  18. Hi, how can i display “quantity label” only in selected pages/post id? Cause i would insert “per-night” label for specific product pages in my website.

  19. Great site with awesome tips. Much appreciated.

    Regarding this, didn’t work for me with variation products. Found a workaround with CSS that works for me in case anyone still needs it:

    .quantity:before {
      content:"Quantity:";
      margin-right:10px
    }
    .quantity input {
      display:inline;
      margin-right:30px
    }
    .woocommerce-variation-add-to-cart {
      margin-top:20px
    }
    1. Thank you Falling Ocean πŸ™‚

  20. Thank you for the information. I am struggling to get Mad Dog’s workaround right. I have changed my wording to “Number of Guests” but when i select the variation then I need the “Number of Guests” text to follow the Qty field. Thanks again.

    1. Hey Kate, thanks for your comment! When you say “I need ____ text to follow the Qty field”, what do you mean? Thanks πŸ™‚

  21. The Code doesnt work on my page. I have no add to cart at my categorie pages and there I get white screen. Whats wrong? Thanks for your good website!

    1. Hey Melanie! If you enable WP_DEBUG on your site, what error do you see?

  22. I tried the code, the code is working but there is one problem.
    I am actually using product addon. The β€œQuantity” text appear between my variation and product addon tab. (screenshot attached below)
    Can you please help me out.

    Here is the Screenshot : https://imgur.com/a/F82Sd

    1. just checked the previous screenshot link was broken
      Here is the new screenshot : https://ibb.co/kC1QGF

      1. Hey Mahesh, which code did you try, mine or the one provided by “Mad Dog” in the comments? Maybe try the 2 and let me know if either works. Cheers!

  23. This didn’t work for me because it displays before variables…..

    But I did the same thing with simple CSS:

    /* ADD “Quantity” in front of Quantity and Add to Cart buttons */
    .woocommerce div.product form.cart div.quantity::before {
    content: “Quantity: “;
    font-weight: bold;
    padding-right: 8px;
    }

    1. Awesome, thanks for sharing this Mad Dog!

      1. I tried the code, the code is working but there is one problem.
        I am actually using product addon. The “Quantity” text appear between my variation and product addon tab. (screenshot attached below)
        Can you please help me out.

        Here is the Screenshot : https://imgur.com/a/F82Sd

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *