Home > Enterprise >  How to change add to cart button text for specific variable products on single page in WooCommerce
How to change add to cart button text for specific variable products on single page in WooCommerce

Time:02-16

I have a few variable products on my website. Among those only on a few specific products, I have added two variations on those products one is "carbon fiber" and other is "unpainted" now what I want is when user clicks on 2nd variation which is "unpainted" the text of add to cart button should change from "add to cart" button to "made to order. I have updated my question to be more specific Below is the code First part code is jQuery to display the variations on my single product page and the second to change the name of variations for specific products:

 window.addEventListener('load', (event) => {



var initialvalue=jQuery('#pa_choose-option').val();
if(initialvalue=="carbon-fibre")
jQuery('.single_variation_wrap .single_add_to_cart_button').html("Add To 
Cart");

jQuery('#pa_choose-option').on('change', function() {
console.log("here");
var initialvalue=jQuery('#pa_choose-option').val();
if(initialvalue!="carbon-fibre")
jQuery('.single_variation_wrap .single_add_to_cart_button').html("Made 
to Order");
 else
jQuery('.single_variation_wrap .single_add_to_cart_button').html("Add To 
Cart");

});


});


add_filter( 'woocommerce_product_single_add_to_cart_text', 
'custom_addtocart_button_text', 10, 2 ); 
function custom_addtocart_button_text( $button_text, $product ) {
// 1st product
if ( $product->get_id() == 6577 ) {
    $button_text = __( 'jawad', 'woocommerce' );
} 
// 2nd product
elseif ( $product->get_id() == 6570 ) {
    $button_text = __( 'kiani', 'woocommerce' );
}
return $button_text;
}

CodePudding user response:

You can use show_variation trigger. try the below code.

function change_add_to_cart_text_based_on_variation(){
    global $post;
    
    $specific_ids = array( 6594, 6577 );

    if( is_product() && in_array( $post->ID, $specific_ids ) ){
        ?>
        <script type="text/javascript">
            (function($){
                $( ".single_variation_wrap" ).on( "show_variation", function ( event, variation ) {
                    if( variation.attributes['attribute_pa_choose-option'] == 'unpainted' ){
                        jQuery('.single_variation_wrap .single_add_to_cart_button').html( "Made to Order" );
                    }else{
                        jQuery('.single_variation_wrap .single_add_to_cart_button').html( "Add To Cart" );
                    }
                } );

                $( document ).on('click', '.reset_variations', function(event) {
                    event.preventDefault();
                    jQuery('.single_variation_wrap .single_add_to_cart_button').html("Add To Cart");
                });

            })(jQuery);
        </script>
        <?php
    }
}
add_action( 'wp_footer', 'change_add_to_cart_text_based_on_variation', 10, 1 );

Tested and works.

enter image description here

  • Related