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 HTML Symbol to Add to Cart Button - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

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 = '» ' . $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 + Storefront Theme

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

Advanced Plugin: Custom Add to Cart Button

The code snippet above will let you add an icon to the add to cart button. If youโ€™d also like to customize the text, then I recommend the free WooCommerce Custom Add To Cart Button plugin.

This well-coded plugin lets you add an icon and either remove the text or change it to whatever you like, such as โ€œAdd to Basketโ€.

It works on its own, or with other plugins from the same company such as WooCommerce Product Table, which is useful for creating quick one-page order forms that benefit from a simplified add to cart button.

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 Visual Hook Guide: Archive / Shop / Cat Pages
    I’ve created a visual HTML hook guide for the WooCommerce Archive Page (which is the same page for the Shop, Category, Tag pages). This visual guide belongs to my “Visual Hook Guide Series“, that I’ve put together so that you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can […]

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

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

  1. Hi Rodolfo,

    I want to ask, how to add some space after the icon?

    Thanks in advance!

    1. There is already a blank space (see the space after the “raquo” semicolon)

  2. The link of “icons from FontAwesome” in this blog ( https://fontawesome.io/icons/ ) is broken.

      1. It doesn’t work when im in incognito

        1. Without incognito it works?

  3. Hi Rodolfo,

    how can you only add the button to the archive pages instead of single product pages?

    Thanks!

    1. Hello Jarno, thanks so much for your comment! Yes, this is definitely possible, 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!

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

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

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

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

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

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

  10. How to make the button bigger?

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

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

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

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

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

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

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 *