-
-
Save deckerweb/cf466e017fd01d503469 to your computer and use it in GitHub Desktop.
<?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 |
woocommerce_product_add_to_cart_text
is still found in /includes/class-wc-product-*.php
and /includes/abstract/abstract-wc-product.php
with no notes about deprecation. The filter still works.
Just as @luboslives commented (thank You) - it doesn't seem depricated to me. It can be found here: https://woocommerce.github.io/code-reference/files/woocommerce-includes-class-wc-product-external.html#source-view.181 , generated "on August 17th, 2021 at 07:59 pm.".
Also, there is no need to use global $product
variable. The function can take two parameters: $text
and $product
. So if one would like to only change text for - let's say - 'simple' products the following simpler code may be used.
function woocommerce_add_to_cart_button_text ( $text, $product ) {
if ( $product->product_type == 'simple' ) {
return 'Add to Cart';
}
return $text;
}
add_filter( 'woocommerce_product_add_to_cart_text' , 'woocommerce_add_to_cart_text' );
Thank You for the gist @deckerweb!
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 --
It can cause a wc_doing_it_wrong()
It should be used like this, otherwise will give error. (Specify number of arguments accepted)
.
function woocommerce_add_to_cart_button_text ( $text, $product ) {
if ( $product->product_type == 'simple' ) {
return 'Add to Cart';
}
return $text;
}
add_filter( 'woocommerce_product_add_to_cart_text' , 'woocommerce_add_to_cart_text' , 10 , 2); // Specify number of arguments accepted.
Its deprecated.