WooCommerce: Add an Icon to the Add to Cart Buttons

Ecommerce is all about user experience, and making it easier for people to add to cart and checkout smoothly. Reducing the number of checkout fields is a great idea for example – as well as graphically communicating your number 1 objective: “please add to cart now!”.

So, how do you add an icon (or an HTML symbol) to the add to cart buttons in WooCommerce? This can be done in two ways – via CSS if you want to show Fontawesome Icons or via PHP if you prefer to use a simple HTML unicode symbol.

Let’s take a look at both methods!

PHP Snippet: Add an HTML Symbol to the Add to Cart Buttons – WooCommerce

You can find the list of HTML symbols here: https://www.w3schools.com/html/html_symbols.asp. Basically, by using some HTML entities, you can print a symbol on the screen such as –> โ‚ฌ <– (FYI, I just used “&”, “euro” and “;” without spaces to print the symbol).

In this example, I want to show a simple symbol called “raquo” (don’t forget to add the “&” and final “;” to actually generate the symbol).


/**
* @snippet       Add an HTML Symbol to the Add to Cart Buttons - WooCommerce
* @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
* @sourcecode    https://businessbloomer.com/?p=73212
* @author        Rodolfo Melogli
* @testedwith    WooCommerce 3.5.1
* @donate $9     https://businessbloomer.com/bloomer-armada/
*/

add_filter( 'woocommerce_product_single_add_to_cart_text', 'bbloomer_add_symbol_add_cart_button_single' );

function bbloomer_add_symbol_add_cart_button_single( $button ) {
$button_new = '&raquo; ' . $button;
return $button_new;
}

Final result:

Add an HTML Symbol to the Add to Cart Buttons

CSS Snippet: Add an Icon to the Add to Cart Buttons – WooCommerce

If you want a better range of icons, Storefront theme already uses a library of icons from FontAwesome to print special icons on the screen. If your theme doesn’t support FontAwesome, you can simply add a script in the header to load the special font.

In this example, I wish to add a “Shopping Cart +” icon provided by Fontawesome, which comes with a code “\f217”.


button.single_add_to_cart_button:before {
    display: inline-block;
    font-family: FontAwesome;
    float: left;
    content: "\f217";
    font-weight: 300;
    margin-right: 1em;
}

Here’s the final result:

Add a FontAwesome Icon to the Add to Cart Buttons

Where to add this snippet?

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 files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

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 Customization?

Check out these free video tutorials!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a 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.

28 thoughts on “WooCommerce: Add an Icon to the Add to Cart Buttons

  1. And here is the working solution for Font Awesome 5 Free:

    .single_add_to_cart_button::before {
    	font-family: 'Font Awesome 5 Free';
    	content: "\f291";
    	font-weight:900;
    	margin-right:.2em;
    }
    
    1. Top!

  2. Hi Rodolfo Thans for the incredible work you’re doing, I am finding it difficult putting “Add To Cart and By NOw” on my woocommerce website which i use Electro Theme for

    1. Hey Willie, 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

  3. Thanks for sharing! But why when I’m trying to add via CSS code it just show me the square? Please, take a look: https://imgur.com/a/3PrjNn1. And there is an added fontawesome code. What can be a problem?

    Thanks

    1. Bohdan, thanks so much for your comment! I just retested this on the latest version of WooCommerce and it still works. Unfortunately this looks like custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R

  4. In your feature image for this post, how did you add the United States and United Kingdom flags and circle selectors? I have also variable products but its a dropdown box ๐Ÿ™

    1. Hello Tim, thanks for your comment ๐Ÿ™‚ That’s added by the affiliate plugin I’m using ๐Ÿ™‚

  5. Hey Rodolfo, finding great resources on your site!
    Well done for sharing your knowledge!!
    Similar to your article above, I am adding icons to the ‘Add to Cart’ Buttons…
    I’ve added a SVG icon to the add to cart button on the single product page – See lines 60-62 on https://gist.github.com/macgraphic/324207402d72ae4163c0e91c2a967391
    But I need to add the same to the archive page buttons. So, that would be theme/woocommerce/loop/add-to-cart.php file? How would I go about adding the

    <?php echo bjas_svg_get_icon( 'bag-fill' ); ?>

    to this?

    1. Hiya Mark ๐Ÿ™‚ In that case, there is a filter called “woocommerce_loop_add_to_cart_link” that you can manipulate via a PHP snippet, without overriding any template. Hope this helps!

  6. Great article thanks! We’ve just launched a free WordPress plugin that lets you add a cart icon to the WooCommerce add to cart button without editing any code, so this will be useful for your less technical readers. You can download the plugin via https://wordpress.org/plugins/woo-custom-add-to-cart-button/

    1. Nice! Thanks Katie ๐Ÿ™‚

  7. How to make the button bigger?

    1. Thanks for your comment Dewi! You will need custom CSS for that ๐Ÿ™‚

  8. Thank you! Better UX now ๐Ÿ™‚

  9. Excellent, thanks a mill, works beautifully :D.

  10. Hi! Rodolfo! I tried the code with CSS Snippet on WordPress and I get the following error message

    (Don’t Panic

    The code snippet you are trying to save produced a fatal error on line 1:

    syntax error, unexpected ‘:’)

    What could be the problem?

    1. Hey Ahmed, thanks for your comment! You have to place CSS in your style.css, not to the PHP ๐Ÿ™‚

  11. Hi and Thanks Rodolfo, and if i want this for all buttons “add to cart”, how i can do it ?

    1. Hey Thierry, this already applies the icon to all ad to cart buttons ๐Ÿ™‚

    2. Not work to the archives page, it’s ok only for the single.

      1. Ah, gotcha! You just need to add one more class (CSS version) or one more filter (PHP version). Hope this helps ๐Ÿ™‚

    3. yeap, thanks Rodolfo.

  12. Rodolfo iโ€™m impressed by your free and full dedication to help woocommerce community.
    Congratulations!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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.