Nicola Mustone

Happiness Lead @ Automattic



How to Change the Add to Cart Text Everywhere in WooCommerce

WooCommerce provides immense flexibility for personalization. One common customization is how to change the Add to Cart text to make it resonate more with their brand or target audience.

If you’ve ever wondered how to modify this text everywhere in your store, you’re in the right place. A small change like this can improve the shopping experience for the customer and increase sales.

Understanding the Importance of Customization

Before we delve into the “How” let’s discuss the “Why” you should have a custom Add to Cart button.

Customizing elements like the Call to Action text can seem trivial. However, in the realm of e-commerce, even such details can influence customer behavior.

Tailoring the WooCommerce Add to Cart text can align more closely with your brand voice and increase sales and user engagement. Similarly, adding buttons with a custom Add to Cart URL can do the same.

A screenshot of a single product page from elementalbeacon.com showing the product image on the left, and the product data on the right. The image showcases a custom Add to Cart button with the text Add to Bag of Holding.
I customized the Add to Cart button on Elemental Beacon to read Add to Bag of Holding. This text is thematic for my D&D shop.

How to Change the Add to Cart Text

Before you make any code changes, have a backup of your site. This will help you restore things in case of unforeseen errors. I recommend using Jetpack Backup for real-time backups and one-click restores.

If you want to change the Add to Cart text in WooCommerce to anything else, the process is straightforward. Thanks to the flexible nature of WooCommerce and WordPress, a code snippet can achieve this.

Add these snippets to your functions.php file.

1. Add to Cart Text on the Single Product Pages

/**
 * @snippet       Change the Add to Cart Text on Single Product Page
 * @author        Nicola Mustone
 * @author_url    https://nicolamustone.blog/2023/10/30/how-change-add-to-cart-text-everywhere-woocommerce/
 * @tested-up-to  WooCommerce 8.4.X
 */
add_filter( 'woocommerce_product_single_add_to_cart_text', 'nm_change_add_to_cart_text' ); 
function nm_change_add_to_cart_text() {
    return 'Add to Bag';
}

2. Add to Cart Text by Category on the Single Product Page

/**
 * @snippet       Change the Add to Cart Text on Single Product Page by Category
 * @author        Nicola Mustone
 * @author_url    https://nicolamustone.blog/2023/10/30/how-change-add-to-cart-text-everywhere-woocommerce/
 * @tested-up-to  WooCommerce 8.4.X
 */
add_filter( 'woocommerce_product_single_add_to_cart_text', 'nm_change_add_to_cart_text_by_category', 10, 2 );
function nm_change_add_to_cart_text_by_category( $default, $product ) {
    $terms = get_the_terms( $product->id, 'product_cat' );

    foreach ( $terms as $term ) {
        switch( $terms->name ) {
            case 'Posters':
                $default = 'Custom Text for Posters Category';
                break;
            case 'Paintings':
                $default = 'Custom Text for Paintings Category';
                break;
            case 'Pictures':
            case 'Frames':
                // You can add multiple cases one after another to use the same text for all of them
                $default = 'Custom Text for Pictures and Frames Categories'
                break;
        }
    }

    return $default;
}

3. Add to Cart Text on Shop Pages

/**
 * @snippet       Change the Add to Cart Text on Shop Pages
 * @author        Nicola Mustone
 * @author_url    https://nicolamustone.blog/2023/10/30/how-change-add-to-cart-text-everywhere-woocommerce/
 * @tested-up-to  WooCommerce 8.4.X
 */
add_filter( 'woocommerce_product_add_to_cart_text', 'nm_change_add_to_cart_text_archives' );  
function nm_change_add_to_cart_text_archives() {
    return 'Add to Bag';
}

4. View Cart Text in the Added to Cart Notices

/**
 * @snippet       Change the View Cart text
 * @author        Nicola Mustone
 * @author_url    https://nicolamustone.blog/2023/10/30/how-change-add-to-cart-text-everywhere-woocommerce/
 * @tested-up-to  WooCommerce 8.4.X
 */
add_filter( 'wc_add_to_cart_message_html', 'nm_change_added_to_cart_view_cart_text', 10 ); 
function nm_change_added_to_cart_view_cart_text( $message ) { 
    $message = str_replace( ' cart', ' Bag', $message );
    return $message; 
}

The Magic Behind the Snippet

At a glance, the snippets above might seem complex, especially if you’re new to WooCommerce customizations. However, they are simpler than they look.

The filters woocommerce_product_single_add_to_cart_text and woocommerce_product_add_to_cart_text target the button text for single product pages and product archives, respectively. The filter wc_add_to_cart_message_html targets instead the “added to cart” message and the “view cart” link that appears after adding a product to the cart.

By applying the custom function, you override the default text with your preferred choice.

Add to Cart Text Blanket Solution

In some cases, the snippets above might just not be enough. This happens when plugins or themes do not use the WooCommerce filters in their templates. One example is with the theme Shoptimizer and the plugin CommerceGuru.

If that is the case, then a blanket solution by text-domain will cover those cases:

/**
 * @snippet       Change the Add to Cart Text Everywhere by Textdomain
 * @author        Nicola Mustone
 * @author_url    https://nicolamustone.blog/2023/10/30/how-change-add-to-cart-text-everywhere-woocommerce/
 * @tested-up-to  WooCommerce 8.4.X
 */
add_filter( 'gettext_woocommerce', 'nm_change_cart_text', 20, 3 );
function nm_change_cart_text( $translated_text, $text, $domain ) {
	$translated_text = str_ireplace( 'cart', ' Bag', $translated_text );
    return $translated_text;
}

// Example for the theme Shoptimizer
add_filter( 'gettext_shoptimizer', 'nm_change_cart_text', 20, 3 );

// Example for the plugin CommerceKit
add_filter( 'gettext_commercegurus-commercekit', 'nm_change_cart_text', 20, 3 );

In the example above, gettext_woocommerce will target any text with the woocommerce text domain. gettext_shoptimizer will target any text with the domain shoptimizer. The last filter, gettext_commercegurus-commercekit, targets CommerceKit.

Before using it, adjust the filter names using the text domain of the plugin or theme you want to target.

Finding the Text Domain

To find the text domain you need to use do this:

  • Themes: download the theme and open its folder. In the main folder, you will find a file named style.css. Open it with your favorite text editor such as Notepad (not Microsft Word or similar).

    At the very beginning of the file, you will find a block starting with /*. Find the line that reads Text Domain: something. The value after Text Domain will be the text domain you’re looking for.
  • Plugins: download the plugin and open its folder. In the main folder, you should find a file with a name similar to the folder name and the extension .php. Open it with your favorite text editor such as Notepad (not Microsft Word or similar).

    At the very beginning of the file, you will find a block starting with /*. Find the line that reads Text Domain: something. The value after Text Domain will be the text domain you’re looking for.
    • If the plugin does not have a file name like its folder, open the PHP files individually until you find it. The file must be in the main folder, not its subfolders.

Note that this method can consume lots of resources on your server. I do not recommend it unless it’s the only solution that would work for you.

The image shows the header block of the file style.css for the theme Shoptimizer, highlighting the text domain line.
The Text Domain of the theme Shoptimizer in the shoptimizer/style.css file.

Wrapping Up

Tweaking the details of your WooCommerce store can offer a more personalized experience for your customers. It’s all part of the broader picture of WooCommerce customization. These changes make your online store truly your own.

Have you tried other customizations on your WooCommerce store? Share your experiences in the comments below, and let’s learn together!

If you found this guide helpful, read our articles about changing the Return to Shop button URL and changing the Continue Shopping redirect URL.


Subscribe to This Blog

Receive new articles from this blog directly in your inbox! No spam guaranteed!

Join 670 other subscribers

Leave a Reply

You Might Also Like These Articles


Contribute to Improving This Blog

Did you enjoy this article? Was it helpful? Contribute to help me write more articles! The funds collected from this blog are reinvested directly into improving my skills or the blog so that I can provide more and better content!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

€1.00
€5.00
€10.00
€5.00
€15.00
€100.00
€5.00
€15.00
€100.00

Or enter a custom amount


Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Don't Miss a Thing!

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Join 670 other subscribers

Continue Reading

Discover more from Nicola Mustone

Subscribe now to keep reading and get access to the full archive.

Continue reading