What would the php code be to change the add to cart text to something unique for each product category, on both the product archives and single product pages using one code snippet if possible? I have six product categories. I am using WooCommerce.
Basically, I have 6 categories, I’d like 5 of them to say "Order" and then 1 of them to say "Book Now"
The 6 categories are:
- Cakes
- Cupcakes
- Halloween
- Sweet Treats
- Petal and Post
- Baking Class
i.e. all of the categories except for "Baking Class" will say Order, while "Baking Class" will say "Book Now".
I’d like to apply to the buttons all over the website, no matter where they are displayed.
I am using Elementor to create custom Product Archives.
Update 25th October, 2022:
I have tried using the code from mujuonly’s answer below. It works except for on the product archive for the Baking Classes category; which I designed using Elementor Pro: https://bunsbakery.co.za/product-category/baking-classes/
Here is the code I used:
add_filter('woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text', 10, 2);
add_filter('woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text', 10, 2);
function woocommerce_custom_product_add_to_cart_text($text, $product) {
if( has_term( array( 'Cakes', 'Cupcakes', 'Halloween', 'Sweet Treats', 'Petal and Post' ), 'product_cat', $product->get_id() ) ){
$text = __('Order', 'woocommerce');
}
if( has_term( array( 'baking-classes' ), 'product_cat', $product->get_id() ) ){
$text = __('Book Now', 'woocommerce');
}
return $text;
}
2
Answers
I have not tested this, but this might be doable like this:
If you want to change it on the single product pages, you should use the filter
woocommerce_product_single_add_to_cart_text
instead.Or both if you want it on both.
Toy around with it, and see if you can get i to work.
UPDATE
So this could be your finalfunction, based on your criteria and mujuonlys answer.
Tested OK in Single product page and Shop page.