Skip to content

Instantly share code, notes, and snippets.

@deckerweb
Created June 3, 2014 14:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deckerweb/cf466e017fd01d503469 to your computer and use it in GitHub Desktop.
Save deckerweb/cf466e017fd01d503469 to your computer and use it in GitHub Desktop.
Custom text for 'woocommerce_product_add_to_cart_text' filter for all product types/ cases.
<?php
/** Do NOT include the opening php tag */
add_filter( 'woocommerce_product_add_to_cart_text' , 'custom_woocommerce_product_add_to_cart_text' );
/**
* Custom text for 'woocommerce_product_add_to_cart_text' filter for all product types/ cases.
*
* @link https://gist.github.com/deckerweb/cf466e017fd01d503469
*
* @global $product
*
* @return string String for add to cart text.
*/
function custom_woocommerce_product_add_to_cart_text() {
global $product;
$product_type = $product->product_type;
switch ( $product_type ) {
case 'external':
return __( 'Buy product', 'woocommerce' );
break;
case 'grouped':
return __( 'View products', 'woocommerce' );
break;
case 'simple':
return __( 'Add to cart', 'woocommerce' );
break;
case 'variable':
return __( 'Select options', 'woocommerce' );
break;
default:
return __( 'Read more', 'woocommerce' );
} // end switch
} // end function
@georgestephanis
Copy link

This can cause some notices -- product_type was called incorrectly. Product properties should not be accessed directly. -- it's likely better to try ->get_type() instead of ->product_type directly --

https://github.com/woocommerce/woocommerce/blob/fd8a5604ba9a0c2792556bc76e9ca5207cd551e1/plugins/woocommerce/includes/legacy/abstract-wc-legacy-product.php#L72

It can cause a wc_doing_it_wrong()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment