I am changing the text of the Add to cart button on shop pages in WooCommerce for specific products:
add_filter( 'woocommerce_product_add_to_cart_text', 'add_to_cart_text' );
function add_to_cart_text( $default ) {
if ( get_the_ID() == 1 ) {
return __( 'Text1', 'your-slug' );
} elseif ( get_the_ID() == 2 ) {
return __( 'Text2', 'your-slug' );
} elseif ( get_the_ID() == 3) {
return __( 'Text3', 'your-slug' );
} else {
return $default;
}
}
If I have two or more IDs that need to have the same button text, how can I add an array of these ids into my function?
3
Answers
You can use in_array() function for that.
You can use
in_array()
PHP conditional function like:It should work.
Also you should use
$product
missing arguments in your hooked function.If you want to do dynamic for future purposes then you can save your
add_to_cart_text
to custom fields and you can retrieve them in thewoocommerce_product_add_to_cart_text
hook. check below code.save your
add_to_cart_text
to custom fileds here.