更改运输方式后重新加载Woocommerce结帐页面

时间:2019-10-30 12:57:22

标签: wordpress woocommerce

一旦我在结帐页面上更改了发运选项,我想显示/隐藏某些字段,但是一旦我手动重新加载页面后它就起作用了,然后我添加了一个jQuery代码来重新加载页面-

jQuery('form.checkout').on('change','input[name^="shipping_method"]',function() {

完整代码-

<?php
// Add custom Theme Functions here


add_filter('woocommerce_checkout_fields', 'xa_remove_billing_checkout_fields');

function xa_remove_billing_checkout_fields($fields) {
     $first_shipping_method ='free_shipping:1'; // Set the desired shipping method to hide the checkout field(s).
     $second_shipping_method ='free_shipping:2'; // Set the desired shipping method to hide the checkout field(s).
     $third_shipping_method ='local_pickup:3'; // Set the desired shipping method to hide the checkout field(s).

    global $woocommerce;
    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = $chosen_methods[0];

    if ($chosen_shipping == $first_shipping_method || $chosen_shipping == $third_shipping_method) {
        unset($fields['billing']['billing_address_1']);
        unset($fields['billing']['billing_address_2']);
    }
    if ($chosen_shipping == $second_shipping_method || $chosen_shipping == $third_shipping_method) {
        unset($fields['billing']['billing_pick_up']);
    }

    ?>

    <script type="text/javascript">
         jQuery('form.checkout').on('change','input[name^="shipping_method"]',function() {
            var val = jQuery( this ).val();
            if (val) {
                  location.reload();
               }
         });
    </script>

    <?php
    return $fields;
}

但这对我不起作用,您可以指导我吗?

1 个答案:

答案 0 :(得分:0)

在应用更改之前刷新页面。 updated_checkout事件运行后重新启动。

<script type="text/javascript">
    let isRefreshPage = false;
    jQuery('form.checkout').on('change','input[name^="shipping_method"]',function() {
        var val = jQuery( this ).val();
        if (val) {
            isRefreshPage = true;
        }
    });
     
    jQuery('body').on('updated_checkout', function(){
        if (isRefreshPage) {
            location.reload();
        }
    });
</script>