簡體   English   中英

僅在 WooCommerce 中顯示特定國家/地區的運費計算器郵政編碼字段

[英]Show shipping calculator postcode field only for specific countries in WooCommerce

我正在使用從 Woocommerce 購物車運輸計算器答案代碼中刪除郵政編碼,我測試過並且工作正常。

但問題是它隱藏了所有國家/地區的運費計算器的郵政編碼。

我想為所有國家/地區隱藏它,除了一個:比利時(BE)。

這可能嗎? 我怎樣才能使它適用於除比利時以外的所有國家/地區。

我認為您可以使用一些 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
}

這將清除郵政編碼字段的值,將其禁用並在所選國家/地區不是比利時時隱藏它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM