I want to change the “add to cart” text button for products that meet the following conditions:
- empty price
- out of stock
The intention is to change the text to “Not available right now”
Here is an example image to clarify my question
This is the code that I use. But I can’t make it work. Any ideas of where I am going wrong?
add_filter( 'woocommerce_after_shop_loop_item', 'price_zero_empty', 9999, 2 ); function price_zero_empty( $price, $product ) { global $product; if ( $stock == 'outofstock' && '' == $product->get_price() || 0 == $product->get_price() ) { add_filter( 'gettext', 'change_readmore_text', 20, 3 ); } function change_readmore_text(){ if (! is_admin() && $domain === 'woocommerce' && $price === 0 && $translated_text === 'Read more') { $translated_text = 'Not available right now';} return $translated_text; }
Advertisement
Answer
To change the text for products that have “no price” and are “out of stock”, you can use the woocommerce_product_add_to_cart_text
filter hook.
So you get:
function filter_woocommerce_product_add_to_cart_text( $add_to_cart_text, $product ) { // Price empty & Product is out of stock if ( empty ( $product->get_price() ) && $product->get_stock_status() == 'outofstock' ) { $add_to_cart_text = __( 'Not available right now', 'woocommerce' ); } return $add_to_cart_text; } add_filter( 'woocommerce_product_add_to_cart_text', 'filter_woocommerce_product_add_to_cart_text', 10, 2 ); add_filter( 'woocommerce_product_single_add_to_cart_text', 'filter_woocommerce_product_add_to_cart_text', 10, 2 );