Posted on

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 a string just in front of the Add to Cart button that says “Quantity: “. As usual, the biggest thing is looking for the right “hook”… but thankfully WooCommerce plugin is full of them and adding a string where we want is so easy πŸ™‚

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" Label in front of Add to Cart Button - WooCommerce
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=21986
 * @author        Rodolfo Melogli
 * @testedwith    WC 3.5.1
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_before_add_to_cart_quantity', 'bbloomer_echo_qty_front_add_cart' );

function bbloomer_echo_qty_front_add_cart() {
 echo '<div class="qty">Quantity: </div>'; 
}

And a couple of CSS lines as well:


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

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

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

  1. 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 πŸ™‚

  2. 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 πŸ™‚

  3. As of 3.0 you should use: woocommerce_before_add_to_cart_quantity

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

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

  5. 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 πŸ™‚

  6. It worked like a charm!

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

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

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

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

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

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

    1. Hey Alex, 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 πŸ™‚

  12. 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 πŸ™‚

  13. 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 πŸ™‚

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

  15. 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 : http://imgur.com/a/F82Sd

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

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

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

    2. 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 : http://imgur.com/a/F82Sd

Questions? Feedback? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.