How to replace add to cart button text for specific category

To replace the “Add to Cart” button text for a specific category in WooCommerce, a custom PHP code snippet is typically used. This code needs to be added to your theme’s functions.php file or, preferably, using a code snippets plugin like “Code Snippets.”

Steps to replace the “Add to Cart” button text for a specific category: 

  • Access your site’s files:
    • Using a Code Snippets Plugin: Install and activate a plugin like “Code Snippets.” This is the recommended method as it avoids directly editing theme files, which can be overwritten during theme updates.
    • Directly editing functions.phpAccess your theme’s functions.php file via FTP or your hosting provider’s file manager. If you are using a child theme, edit the functions.php file of the child theme.
  • Add the code snippet:
    Add the following PHP code to your chosen location (either the snippets plugin or functions.php):
    add_filter( ‘woocommerce_product_add_to_cart_text’, ‘custom_add_to_cart_text_by_category’, 99, 2 );
    function custom_add_to_cart_text_by_category( $text, $product ) {
    // Replace ‘your-category-slug’ with the actual slug of your category
    if ( has_term( ‘your-category-slug’, ‘product_cat’, $product->get_id() ) ) {
    return ‘Your Custom Text’; // Replace with your desired button text
    }
    return $text;
    }
    • Customize the code:

      • Replace 'your-category-slug' with the actual slug of the product category you want to target. You can find the category slug in your WordPress admin panel under “Products” > “Categories.”
      • Replace 'Your Custom Text' with the text you want to display on the “Add to Cart” button for products in that specific category.
    • Save and Activate:

      • If using a snippets plugin, save and activate the snippet.
      • If editing functions.php directly, save the file.
    Explanation of the code:

    • add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_text_by_category', 99, 2 );: This line hooks into the woocommerce_product_add_to_cart_text filter, which allows modification of the “Add to Cart” button text. The 99 sets a high priority to ensure this filter runs after others, and 2 indicates that the function accepts two arguments ($text and $product).
    • function custom_add_to_cart_text_by_category( $text, $product ): This defines the function that will modify the text. It takes the current button text ($text) and the product object ($product) as arguments.
    • if ( has_term( 'your-category-slug', 'product_cat', $product->get_id() ) ): This conditional statement checks if the current product belongs to the specified category.
    • return 'Your Custom Text';: If the product is in the target category, this line returns the new custom text for the button.
    • return $text;: If the product is not in the target category, the original “Add to Cart” text is returned.
    Note: For multiple categories, you can add more if or elseif statements within the function, or use an array of category slugs to apply the same text to several categories.
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *