1. Home
  2. Help Center
  3. WooCommerce
  4. Customization
  5. Change “add to cart” text

WooCommerce has some filters that allows to change the add to cart text based on the context.

All PHP snippets must be added to the child theme functions file wp-content/themes/goya-child/functions.php

Single product

There are two basic filters to change the add to cart button texts. One for “add to cart” texts in in single product (woocommerce_product_single_add_to_cart_text):

// Change 'add to cart' text on single product page
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_text' );
function custom_add_to_cart_text() {
 return __( 'Buy this now', 'goya' );
}Code language: JavaScript (javascript)

Product Archive

The other filters is for the products in the catalog/loop (woocommerce_product_add_to_cart_text):

// Change 'add to cart' text on archive product page
add_filter( 'woocommerce_product_add_to_cart_text', 'custom_archive_add_to_cart_text' );
function custom_archive_add_to_cart_text() {
 return __('Buy this', 'goya');
}Code language: JavaScript (javascript)

Keep in mind that the snippet overrides all “Add to cart” texts. Even those which were originally “Select options” (variable products) or “Read more” (un-purchasable products – no price or out of stock).

If you want more control, include the second argument to these filters, the $product object. Use the $product object to conditionally control the output.

For example, by product type or whether or not product can be purchased:

// Change add to cart text on archives depending on product type
add_filter( 'woocommerce_product_add_to_cart_text' , 'custom_woocommerce_product_add_to_cart_text' );
function custom_woocommerce_product_add_to_cart_text() {
 global $product;
 
 $product_type = $product->get_type();
 
 switch ( $product_type ) {
  case 'external':
   return __( 'Take me to their site!', 'woocommerce' );
  break;
  case 'grouped':
   return __( 'VIEW THE GOOD STUFF', 'woocommerce' );
  break;
  case 'simple':
   return __( 'WANT. NEED. ADD!', 'woocommerce' );
  break;
  case 'variable':
   return __( 'Select the variations, yo!', 'woocommerce' );
  break;
  default:
   return __( 'Read more', 'woocommerce' );
 }
 
}Code language: PHP (php)

Change specific product

You may want to change the text for a certain product only. If so, use the following but change the 299 to the product ID of the product in question:

// Change 'add to cart' text on single product page (only for product ID 299)
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_id_add_to_cart_text' );
function custom_id_add_to_cart_text( $default ) {
 if ( get_the_ID() == 299 ) {
  return __( 'Buy offer!', 'goya' );
 } else {
  return $default;
 }
}Code language: PHP (php)

Change products by category

Let’s say you wanted to only change the add to cart text for products in a certain product category. Just change the 19 to be the ID of the category in mention:

// Change 'add to cart' text on single product page (only for category ID 17)
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_cat_add_to_cart_text' );
function custom_cat_add_to_cart_text( $default ) {
 global $post;
 $terms = get_the_terms( $post->ID, 'product_cat' );
 if ( array_key_exists( 19, $terms ) ) {
  return __( 'Buy offer', 'your-slug' );
 } else {
  return $default;
 }
}Code language: PHP (php)

Was this article helpful?

Related Articles