Amaury Lemal
Amaury Lemal

Reputation: 25

Change add to cart button text on click event in Woocommerce single product pages

I am trying to swap the name of the woocommerce "Add to cart" button when clicked to "Adding..." how can I add the onclick behavior to the function below?( without editing the woocommerce template file.)

add_filter( 'woocommerce_product_single_add_to_cart_text', 'quailstudio_add_to_cart_text' );
function quailstudio_add_to_cart_text() {
        return __( 'Adding...', 'your-slug' );
}

Upvotes: 1

Views: 4522

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

The following code will replace the normal add to cart text button by a custom text on click event:

add_action( 'wp_footer', 'single_add_to_cart_event_text_replacement' );
function single_add_to_cart_event_text_replacement() {
    global $product;

    if( ! is_product() ) return; // Only single product pages
    if( $product->is_type('variable') ) return; // Not variable products
    ?>
        <script type="text/javascript">
            (function($){
                $('button.single_add_to_cart_button').click( function(){
                    $(this).text('<?php _e( "Adding...", "woocommerce" ); ?>');
                });
            })(jQuery);
        </script>
    <?php
}

This code goes on function.php file of your active child theme (or active theme). tested and works.

Upvotes: 3

Related Questions