Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting and stoping Ajax Overlay spinner in Woocommerce

I have developed custom payment gateway which will delay the process payment through ajax. I would like to show the same spinner which is currently used in woocommerce.

That is an extract of my jQuery code:

function callAjax(){
    jQuery.ajax({
        type : "POST",
        url: '<?php echo site_url().'/?wc-api=custom_ function'; ?>',
        data: response,
        //data: {action:'gateway'},
        dataType : "json",
        cache: false,
        success: function(response) {                               
            //alert("Your vote could not be added");
            //alert(response);
            flag = true;
            // window.location = response.redirect;
            //console.log(response);
            return false;
        }
    }); 
}

setTimeout(
    function(){ callAjax(); 
}, 3000);

So I would like to make this:

please check with the image

How to start and stop in checkout page the ajax Woocommerce overlay spinners?

like image 770
mike Avatar asked Feb 05 '19 14:02

mike


1 Answers

Woocommerce Uses jQuery BlockUI Plugin to make a blocking overlay with an animate spinner on some jQuery events and on specific pages.

Here below is an example on the checkout page, that will activate woocommerce spinners after a delay of 2 seconds once page is loaded and will stop them after 3 seconds:

add_action('wp_footer', 'spinners_example_on_checkout_jquery_script');
function spinners_example_on_checkout_jquery_script() {
    if ( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        // Targeting checkout checkout payment and Review order table like Woocommerce does.
        var a = '.woocommerce-checkout-payment, .woocommerce-checkout-review-order-table';

        // Starting spinners with a delay of 2 seconds
        setTimeout(function() {
            // Starting spinners
            $(a).block({
                message: null,
                overlayCSS: {
                    background: "#fff",
                    opacity: .6
                }
            });

            console.log('start');

            // Stop spinners after 3 seconds
            setTimeout(function() {
                // Stop spinners
                $(a).unblock();

                console.log('stop');
            }, 5000);
        }, 2000);
    });
    </script>
    <?php
    endif;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

The jQuery BlockUI Plugin official documentation.

like image 85
LoicTheAztec Avatar answered Sep 27 '22 21:09

LoicTheAztec