在 Woocommerce 中启动和停止 Ajax Overlay 微调器

mik*_*ike 7 php ajax wordpress jquery woocommerce

我开发了自定义支付网关,它将通过ajax延迟处理支付。我想展示目前在 woocommerce 中使用的相同微调器。

这是我的 jQuery 代码的摘录:

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);
Run Code Online (Sandbox Code Playgroud)

所以我想做这个:

请检查图片

如何在结帐页面中启动和停止 ajax Woocommerce 叠加旋转器?

Loi*_*tec 8

Woocommerce 使用jQuery BlockUI 插件在某些 jQuery 事件和特定页面上使用动画微调器制作阻塞覆盖。

下面是结账页面上的示例,页面加载后,它将在 2 秒延迟后激活 woocommerce 旋转器,并在 3 秒后停止它们:

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;
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。

jQuery BlockUI 插件官方文档。