0

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

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.