How to Rename Add to Cart Button by Category
If you run a WooCommerce store, you might want to make your shop stand out by customizing the “Add to Cart” button text for certain products. For example, maybe you want the button to say “Grab This Now” for products in a special category like “Limited Edition” to create excitement. This article shows you how to easily change the “Add to Cart” button text for products in one specific category using a simple code snippet. Don’t worry if you’re not a coder, this guide is beginner-friendly and walks you through everything.
Below is the code you’ll use to change the “Add to Cart” button text for products in a specific category. It works on both the shop page (where products are listed) and the single product page.
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_category_id_add_to_cart_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_category_id_add_to_cart_text', 10, 2 );
function custom_category_id_add_to_cart_text( $text, $product ) {
// Check if $product is a valid WooCommerce product to avoid errors
if ( ! is_a( $product, 'WC_Product' ) ) {
return $text; // Return default text if product is invalid
}
// Set your WooCommerce product category ID below
$category_id = 123; // Replace 123 with your category ID (find it in WooCommerce > Products > Categories)
$category_id = absint( $category_id ); // Ensure category ID is a valid integer
// Get all category IDs assigned to the product
$product_cat_ids = wp_get_post_terms( $product->get_id(), 'product_cat', [ 'fields' => 'ids' ] );
// Check for errors when retrieving categories to avoid warnings
if ( is_wp_error( $product_cat_ids ) ) {
return $text; // Return default text if category retrieval fails
}
// Check if the product belongs to the specified category
if ( in_array( $category_id, $product_cat_ids, true ) ) {
return 'Grab This Now'; // Change this to your custom button text
}
// Return default text for products not in the specified category
return $text;
}How It Works and What to Expect
This code is like a little instruction manual for your WooCommerce store. It tells your website to check if a product belongs to a specific category and, if it does, change the “Add to Cart” button text to something you choose. Here’s a simple breakdown of how it works and what you’ll see:
What It Does
- The code targets products in one specific category (you’ll pick the category by its ID).
- For those products, it changes the “Add to Cart” button text to something custom, like “Grab This Now.”
- Products in other categories will keep the default “Add to Cart” text.
Where It Works
- The new button text will appear on the shop page (where your products are listed) and the single product page (where customers see product details).
- It works for simple products (like a t-shirt) and variable products (like a t-shirt with different sizes).
How to Set It Up
- Find Your Category ID: In your WordPress dashboard, go to WooCommerce > Products > Categories. Hover over the category you want to target, click “Edit,” and look at the URL. You’ll see something like tag_ID=123. The number (e.g., 123) is your category ID.
- Edit the Code: In the code above, replace 123 with your category ID. Change ‘Grab This Now’ to whatever text you want (e.g., “Shop Now” or “Add to Basket”).
- Add the Code: Copy the code and paste it into your theme’s functions.php file. You can do this by going to Appearance > Theme File Editor in WordPress, or use a plugin like Code Snippets for an easier and safer way. If you’re not sure how to do this, ask your developer or check the WordPress documentation for adding code snippets.
- Optional: Use a Child Theme: To avoid losing your changes when your theme updates, consider adding the code to a child theme’s functions.php file.
What to Expect
- After adding the code, visit your shop page and single product pages. Products in the chosen category will show your custom button text (e.g., “Grab This Now”) instead of “Add to Cart.”
- Products in other categories will still show the default “Add to Cart” text.
- The code is built to avoid errors, so it won’t break your site even if something goes wrong (like an invalid product or category).
Testing It
- Check a product in your target category on both the shop page and its single product page to confirm the button text has changed.
- Check a product in a different category to make sure it still says “Add to Cart.”
- If the text doesn’t change, double-check your category ID and ensure the code is added correctly. You can also enable WordPress debug mode to spot any issues (ask your developer if you’re unsure how).
Why It’s Safe
- The code includes checks to prevent errors, like ensuring the product is valid and the category data is correctly retrieved.
- It only changes the button text and doesn’t affect how your store works (e.g., adding products to the cart still functions normally).
Tips for Success
- Keep It Simple: Choose a short, catchy button text that fits your brand, like “Buy Now” or “Get It Fast.”
- Test After Changes: Always test your shop and product pages after adding the code to make sure everything looks good.
- Backup Your Site: Before adding code, back up your WordPress site in case you need to undo changes.
- Ask for Help: If you’re new to coding, consider using a plugin like Code Snippets to add the code safely, or ask a developer for assistance.
By using this snippet, you can make your WooCommerce store feel more unique and engaging for your customers. It’s a small change that can make a big difference in how your products are presented!
Written by Milan on 29th June 2025

