【问题标题】:Show shipping calculator postcode field only for specific countries in WooCommerce仅在 WooCommerce 中显示特定国家/地区的运费计算器邮政编码字段
【发布时间】:2020-12-27 04:54:32
【问题描述】:

我正在使用Remove postcode from Woocommerce cart shipping calculator 答案代码,我测试过并且工作正常。

但问题是它隐藏了所有国家/地区的运费计算器的邮政编码。

我想要为所有国家/地区隐藏它,除了一个:比利时 (BE)。

这可能吗?我怎样才能使它适用于除比利时以外的所有国家/地区。

【问题讨论】:

    标签: php wordpress woocommerce cart shipping


    【解决方案1】:

    我认为您可以使用一些 jQuery 根据所选国家/地区动态隐藏和显示邮政编码字段:

    add_action( 'wp_footer', 'show_shipping_calculator_postcode_field_based_on_country', 50 );
    function show_shipping_calculator_postcode_field_based_on_country() {
        if ( ! is_cart() ) return;
        ?>
        <script type='text/javascript'>
            jQuery(function($){
                $(document.body).on('change', 'select[name="calc_shipping_country"]', function() {
    
                    let country = $(this).find( 'option:selected' ).val();
                    let postcode = $(this).closest( 'p#calc_shipping_country_field' ).siblings( 'p#calc_shipping_postcode_field' ).find('input');
    
                    if ( country !== 'BE' ) {
                        postcode.prop('disabled', true);
                        postcode.attr('value', '');
                        postcode.hide();
                    } else {
                        postcode.prop('disabled', false);
                        postcode.attr('value', '<?php echo WC()->customer->get_shipping_postcode(); ?>');
                        postcode.show();
                    }
                });
            });
        </script>
        <?php
    }
    

    这会清除邮政编码字段的值,禁用它并在所选国家不是比利时时隐藏它。

    【讨论】:

      猜你喜欢
      • 2017-05-31
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 2020-08-28
      • 2017-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多