duanlv2788 2017-10-13 15:04
浏览 63
已采纳

覆盖前端JS for Woocommerce Checkout

I need to hide the billing address, city, state and zip fields when the checkout page is loaded in order to use our address validation script. The code below accomplishes what we want but it does it too late in the process. You see the fields until it runs the updated_checkout event which is too late in the process. However running it on init_checkout seems to be too early as there is something else forcing those fields to be displayed. Is there something in between init_checkout and updated_checkout that I should be watching for?

jQuery( "body" ).on( "updated_checkout", function() {
  jQuery("#billing_country_field,
  #billing_address_1_field
  #billing_address_2_field,
  #billing_city_field,
  #billing_state_field,
  #billing_postcode_field").css("display", "none");
});
  • 写回答

1条回答 默认 最新

  • dongtou9934 2017-10-14 07:32
    关注

    As the DOM is first loaded, the fields will appear anyway.

    I have tried with all available javascript events delegated from "body": added_to_cart adding_to_cart change click country_to_state_changed country_to_state_changing init_checkout update_checkout updated_wc_div wc_fragment_refresh wc_fragment_refreshed. The fields will always appear a bit.

    The only way is to hide checkout form when loading with some CSS rules that you will add to your active theme styles.css file:

    form.checkout.woocommerce-checkout{
        visibility:hidden;
        opacity:0;
    }
    

    And a bit of jQuery that will first hide your fields, before displaying the checkout form:

    add_filter( 'wp_footer', 'custom_checkout_script' );
    function custom_checkout_script( ){
        if( ! is_checkout() ) return;
        ?>
        <script type="text/javascript">
        jQuery(function($){
            var events = 'update_checkout',
                billingFields = '#billing_country_field,#billing_address_1_field,#billing_address_2_field';
            billingFields += ',#billing_city_field,#billing_state_field,#billing_postcode_field';
    
            $('body').on( events, function(){
                $(billingFields).hide( 0,function(){
                    $('form.checkout.woocommerce-checkout').css('visibility','visible').fadeIn({ duration: 1000 });
                });
            });
        });
        </script>
        <?php
    }
    

    This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    All code is tested on Woocommerce 3+ and works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行