Home » WooCommerce: Edit “Add to Cart” Text by Product Category

WooCommerce: Edit “Add to Cart” Text by Product Category

by Tutor Aspire

Today we take a look at the WooCommerce “Add to Cart” buttons. What if you wanted to change the “Add to Cart” text depending on the Product Category? For example, you may want to show “Buy Now” for books and “Add to Basket” for cds.

WooCommerce: Change Add to Cart Text depending on the Category
WooCommerce: Change Add to Cart Text depending on the Category

PHP Snippet: Change “Add to Cart” Text by Product Category (2 Categories Only)

/**
 * @snippet       WooCommerce: Edit "Add to Cart" Text by Product Category
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 6
 * @donate $9     https://tutoraspire.com
 */

add_filter( 'woocommerce_product_add_to_cart_text', 'tutoraspire_archive_custom_cart_button_text' );

function tutoraspire_archive_custom_cart_button_text() {
   global $product;
   if ( has_term( 'category1', 'product_cat', $product->get_id() ) ) {
      return 'Category 1 Add Cart';
   } else {
      return 'Category 2 Buy Now';
   }
}

PHP Snippet: Change “Add to Cart” Text by Product Category (Improved Snippet by Fabio Tielen)

/**
 * @snippet       WooCommerce: Edit "Add to Cart" Text by Product Category
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 6
 * @donate $9     https://tutoraspire.com
 */

add_filter( 'woocommerce_product_add_to_cart_text', 'tutoraspire_archive_custom_cart_button_text' );

function tutoraspire_archive_custom_cart_button_text() {
   global $product;
   $terms = get_the_terms( $product->get_id(), 'product_cat' );
   foreach ( $terms as $term ) {
      $product_cat = $term->name;
      break;
   }
   switch( $product_cat ) {
      case 'category1';
         return 'Category 1 button text'; break;
      case 'category2';
         return 'Category 2 button text'; break;
      // case 'category3'; etc...
      // return 'Category 3 button text'; break;
      default;
         return 'Default button text when no match found'; break;
    }
}

You may also like