2015-02-09 141 views
2

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

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; 
} 

但我已经将如何做到这一点的动态变化国家

回答

0

我一直在试图获得非常类似的东西不知道,而是在选择特定送货方式时显示自定义字段。

以前我有以下jquery通过将它添加到cart-shipping.php模板成功地工作,但我似乎无法让它在'状态'字段上工作。或许,这可以帮助(我们俩)以某种方式到达我们俩以后就是答案......

<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> 
+1

幸得#Sonic顾问 - 上面的代码修改送货方式来显示/隐藏类。我根据上面的代码发布了一个答案,该代码根据帐单和发货国家修改了表单字段。谢谢索尼克顾问! – Jason 2015-12-15 22:05:54

2

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

我打算使用钩子来限制post_code字段为5,但许多国家的邮政编码字符串较长(如加拿大,即6)。感谢#Sonic Advisor。我能够迅速地修改代码以选择性地改变post_code表单字段的最大长度属性,如下图所示:

<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>