首页 文章

基于国家/地区的Woocommerce自定义结帐字段

提问于
浏览
2

我有一个使用woocommerce的电子商务网站在结账页面我需要激活一个自定义必填字段“Codice Fiscale”如果开票国家设置为“意大利”,否则必须删除额外字段我的孩子主题函数中的代码.php是

add_filter( 'woocommerce_checkout_fields' , 'field_cfpiva1' );

function field_cfpiva1( $fields ) {
 $fields['billing']['billing_cf'] = array(
  'label'     => __('Codice Fiscale', 'woocommerce'),
  'placeholder'   => _x('Codice Fiscale', 'placeholder', 'woocommerce'),
  'required'  => false,
  'class'     => array('form-row-wide'),
  'clear'     => true
 );

 return $fields;
}

add_filter( 'woocommerce_admin_billing_fields' , 'admin_field_cfpiva1' );

function admin_field_cfpiva1( $fields ) {
 $fields['cf'] = array(
  'label' => __('Codice Fiscale', 'woocommerce'),
  'show'  => true
 );
 return $fields;
}

但我不知道如何在国家变化上动态地做到这一点

2 回答

  • 2

    我一直在尝试实现非常相似的东西,而是在选择特定的送货方式时显示自定义字段 .

    以前我将以下jquery成功地添加到cart-shipping.php模板中,但我似乎无法让它在'state'字段上工作 . 也许这可能有助于(我们俩)以某种方式达到我们所有人的答案......

    <script>
        $(document).ready(function (){
    
            if ($('#shipping_method_0').val() == 'flat_rate:delivered-vic-only'){
                $('#newfield').show();
            }
    
            $('#shipping_method_0').on('change',function() {
                    if ($('#shipping_method_0').val() == 'flat_rate:delivered-vic-only'){
                    $('#newfield').show();  
                } else {
                $('#newfield').hide();
    
                }
            })
        })
        </script>
    
  • 0

    我知道这个问题有点旧,但这是我改变邮政编码字段最大长度的解决方案 . 我的客户使用的是WooCommerce Table Rates运费插件,而在美国,如果输入的邮政编码包含完整的9位数字(xxxxx-xxxx),则该插件无法正确计算运费 . 我们向同一州的人收取国际费率 .

    我打算使用钩子将post_code字段限制为5,但许多国家/地区的邮政编码字符串较长(如加拿大,即6) . 感谢#Sonic Advisor . 我能够快速修改代码,有选择地更改post_code表单字段的maxlength属性,如下所示:

    <script>
    //Limit zip code to 5 digits for United States ONLY
        jQuery(document).ready(function (){
    
             if (jQuery('#billing_country').val() == 'US'){
                jQuery('#billing_postcode').attr('maxlength','5');
    
            }
    
            jQuery('#billing_country').on('change',function() {
                    if (jQuery('#billing_country').val() == 'US'){
                    jQuery('#billing_postcode').attr('maxlength','5');  
                } else {
                jQuery('#billing_postcode').attr('maxlength','15');
    
                }
            })
    
            if (jQuery('#shipping_country').val() == 'US'){
                jQuery('#shipping_postcode').attr('maxlength','5');
    
            }
    
            jQuery('#shipping_country').on('change',function() {
                    if (jQuery('#shipping_country').val() == 'US'){
                    jQuery('#shipping_postcode').attr('maxlength','5');  
                } else {
                jQuery('#shipping_postcode').attr('maxlength','15');
    
                }
            })
        })
        </script>
    

相关问题