Change multiple WooCommerce Add to Cart Button Text

I'm trying to target a few of our WooCommerce product pages "Add to Cart" buttons to say different things.

This code works for targeting one product and leaving the rest default:

    add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); 

    function woo_custom_cart_button_text( $text ) {
    global $product;

    if ( 123 === $product-id ) {
       $text = 'Product 123 text';
    }
    return $text;
    }

I tried adding an elseif statement after the first if but it's not resolving:

    add_filter( 'woocommerce_product_add_to_cart_text', 'woo_custom_cart_button_text' );  // CHANGES INLINE BUY BTN BANNER
    add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); // CHANGES SINGLE PRODUCT PAGE BUY BTN

    function woo_custom_cart_button_text( $text ) {
    global $product;

    if ( 4471 === $product-id ) { //SUBSCRIBE
       $text = 'Join the Kindred';
    } elseif ( 4297 === $product-id ) { //SOUND JOURNEY
       $text = 'Toss in your tipi';
    }
    return $text;
    }

How do I need to tweak the code to allow me to target individual products before defaulting to the normal "Add to Cart" text? Gracias!

Topic woocommerce-offtopic functions e-commerce Wordpress

Category Web


I think that you want both your if and elseif to run but elseif is triggered only once the if returns false.

function woo_custom_cart_button_text( $text ) {
global $product;

if ( 4471 === $product->id ) { //SUBSCRIBE
   $text = 'Join the Kindred';
}
if ( 4297 === $product->id ) { //SOUND JOURNEY
   $text = 'Toss in your tipi';
}
else
{
   // Is all others but none of the above
}
return $text;
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.