PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 3, 2022

[FIXED] How to change add to cart button text for specific variable products on single page in WooCommerce

 March 03, 2022     php, woocommerce, wordpress     No comments   

Issue

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

Solution

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



Answered By - Bhautik
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing