Change multiple WooCommerce Add to Cart Button Text

Question

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!

0
, , Tiger Danky 2 years 2019-12-28T12:02:58-05:00 0 Answers 105 views 0

Leave an answer

Browse
Browse