How to change a WooCommerce add to cart button in Product List loop but depending on the product type, like for example:
- For products with Variations I want a text in add to cart button to: "Show product"
- For Simple prodcuts "Show product"
- For products Out of stock: "Unavailable"
I tried with below code but doesn’t work:
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product ) {
$button_text = __( "Out of stock", "woocommerce" );
return '<a class="view-product" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
if( ! $product->managing_stock() && ! $product->is_in_stock() ) {
return $button;
}
if( $product->is_type( 'variable' ) ) return $button;
}
2
Answers
Try the following instead:
Code goes in functions.php file of the active child theme (or active theme). It should work
To change woocommerce Book Now button I installed this plugin https://wordpress.org/plugins/button-customizer-for-woocommerce/ and added 2 filters to functions.php and finally, it works