Posted on

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.2.2
*/

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

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

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

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

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

  4. 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 ๐Ÿ™‚

  5. How to make the button bigger?

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

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

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

  8. 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 ๐Ÿ™‚

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

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

    4. yeap, thanks Rodolfo.

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

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.