WooCommerce: Replace the “Add to cart” button text on Archive Pages

Are you looking to learn the basics of WooCommerce development?
My book "Learning WooCommerce Development by Example" will teach you what you need in a fast and friendly way!

All of the archive pages (the shop page, category pages, the search page etc) have an “Add to cart” button displayed below the products

As you can see from the image not all products have a button that reads “Add to cart”, variable products have a button that reads “Select options”, in this article we will take a look at how to change the button text so we can make it display text of our choosing.

Changing the “Add to Cart” Button Text for a Single Product

The button text can be changed by hooking into the “woocommerce_product_add_to_cart_text” filter, so let’s write an example that will change the text to “More details” for the “T-Shirt with Logo”

function hwn_change_add_to_cart_text($cart_text, $product) {
    $tshirt_with_logo_product_id = 32;
    return ($product->id == $tshirt_with_logo_product_id ? "More details" : $cart_text);
}
add_filter('woocommerce_product_add_to_cart_text', 'hwn_change_add_to_cart_text',10,2);

In the example above we hook into the woocommerce_product_add_to_cart_text filter with a function named hwn_change_add_to_cart_text.

Inside the function we set-up a variable named $tshirt_with_logo_product_id and set it’s value to 32, this is the id of the product in my test store, the value may be different in your test store.

The function then checks if the product passed into the hwn_change_add_to_cart_text function has the same id as the t-shirt, if it does, it returns the text “More details” if it doesn’t then the function returns the original value passed in by the filter.

Note that when we hook into the hwn_change_add_to_cart_text function, we pass a priority and number of arguments parameters as the woocommerce_product_add_to_cart_text only takes one argument by default.

Once we have added the code snippet above to a store then the text on the “Add to cart” button of the t-shirt with logo should should change

Changing the “Add to Cart” Button Text for all Products in a Category

It’s only a small change to amend our function so it changes the “Add to cart” button text for all products in a category

function hwn_change_add_to_cart_text_for_all_tshirts($cart_text, $product) {
    if( has_term( array('tshirts'), 'product_cat', $product->id ) ) {
        return "More details";
    }
    return $cart_text;
}
add_filter('woocommerce_product_add_to_cart_text', 'hwn_change_add_to_cart_text_for_all_tshirts',10,2);

The code in the function above is very similar to our first function but we introduce some new if logic to check for the “tshirts” category rather than a specific product id. You can read more about using the has_term  function in our article about removing the “Add to cart” button for categories.

Once the code above is added then the “Add to cart” button text should change for all the products in the t-shirt category

Following this change, the button text has changed for all product types, so let’s make one final change so we only change the text for simple products in the “t-shirt” category

Changing the “Add to Cart” Button Text for all Products of a Certain Type

To make this change we’ll add an additional change to the if logic to check the product type

function hwn_change_add_to_cart_text_for_all_simple_product_tshirts($cart_text, $product) {
    if( has_term( array('tshirts'), 'product_cat', $product->id ) &&  $product->is_type('simple')) {
        return "More details";
    }
    return $cart_text;
}
add_filter('woocommerce_product_add_to_cart_text', 'hwn_change_add_to_cart_text_for_all_simple_product_tshirts',10,2);

Once we have added the $product->is_type('simple') check to the if statement, then the “Add to cart” button for the variable product returns to it’s original value

And that’s it, as always, if you have any questions or queries about the article then please don’t hesitate to leave me a comment in the section below.

Leave a Comment