2016-10-10 98 views
1

我找到了解决这个问题,并添加代码在这里找到:Hide billing address from checkout page but keep the information添加帐单地址,Woocommerce注册页面

“2)添加结算领域在用户注册我的帐户页面”

这成功添加下账单地址到注册表格。登记表中的字段将数据完美地送入Woocommerce数据库。

这就是说,“国家”下拉菜单不会更新时,我更改“国家”下拉菜单。状态下拉菜单确实会根据在结帐页面上选择的国家/地区以及“我的帐户”区域中的编辑地址,但不在新的注册页面上,正确更新。因此,目前它的注册页面只适用于美国。

有关代码如何更改以正确更新“状态”下拉更新的任何建议将非常有帮助。

谢谢! 马克

+0

对不起,目前还不清楚。你需要一个答案吗? –

+0

是的,任何帮助,将不胜感激。仍试图解决这个问题。 –

+0

我也对这个问题的答案感兴趣。 – JPashs

回答

1

要在注册页面上添加开票字段,你可以使用这个片段:

<?php 

// Function to check starting char of a string 
function startsWith($haystack, $needle){ 
    return $needle === '' || strpos($haystack, $needle) === 0; 
} 


// Custom function to display the Billing Address form to registration page 
function zk_add_billing_form_to_registration(){ 
    global $woocommerce; 
    $checkout = $woocommerce->checkout(); 
    ?> 
    <?php foreach ($checkout->get_checkout_fields('billing') as $key => $field) : ?> 

     <?php if($key!='billing_email'){ 
      woocommerce_form_field($key, $field, $checkout->get_value($key)); 
     } ?> 

    <?php endforeach; 
} 
add_action('woocommerce_register_form_start','zk_add_billing_form_to_registration'); 

// Custom function to save Usermeta or Billing Address of registered user 
function zk_save_billing_address($user_id){ 
    global $woocommerce; 
    $address = $_POST; 
    foreach ($address as $key => $field){ 
     if(startsWith($key,'billing_')){ 
      // Condition to add firstname and last name to user meta table 
      if($key == 'billing_first_name' || $key == 'billing_last_name'){ 
       $new_key = explode('billing_',$key); 
       update_user_meta($user_id, $new_key[1], $_POST[$key]); 
      } 
      update_user_meta($user_id, $key, $_POST[$key]); 
     } 
    } 

} 
add_action('woocommerce_created_customer','zk_save_billing_address'); 


// Registration page billing address form Validation 
function zk_validation_billing_address(){ 
    global $woocommerce; 
    $address = $_POST; 
    foreach ($address as $key => $field) : 
     // Validation: Required fields 
     if(startsWith($key,'billing_')){ 
      if($key == 'billing_country' && $field == ''){ 
       $woocommerce->add_error('<strong>' . __('ERROR', 'woocommerce') . '</strong>: ' . __('Please select a country.', 'woocommerce')); 
      } 
      if($key == 'billing_first_name' && $field == ''){ 
       $woocommerce->add_error('<strong>' . __('ERROR', 'woocommerce') . '</strong>: ' . __('Please enter first name.', 'woocommerce')); 
      } 
      if($key == 'billing_last_name' && $field == ''){ 
       $woocommerce->add_error('<strong>' . __('ERROR', 'woocommerce') . '</strong>: ' . __('Please enter last name.', 'woocommerce')); 
      } 
      if($key == 'billing_address_1' && $field == ''){ 
       $woocommerce->add_error('<strong>' . __('ERROR', 'woocommerce') . '</strong>: ' . __('Please enter address.', 'woocommerce')); 
      } 
      if($key == 'billing_city' && $field == ''){ 
       $woocommerce->add_error('<strong>' . __('ERROR', 'woocommerce') . '</strong>: ' . __('Please enter city.', 'woocommerce')); 
      } 
      if($key == 'billing_state' && $field == ''){ 
       $woocommerce->add_error('<strong>' . __('ERROR', 'woocommerce') . '</strong>: ' . __('Please enter state.', 'woocommerce')); 
      } 
      if($key == 'billing_postcode' && $field == ''){ 
       $woocommerce->add_error('<strong>' . __('ERROR', 'woocommerce') . '</strong>: ' . __('Please enter a postcode.', 'woocommerce')); 
      } 
      /* 
      if($key == 'billing_email' && $field == ''){ 
       $woocommerce->add_error('<strong>' . __('ERROR', 'woocommerce') . '</strong>: ' . __('Please enter billing email address.', 'woocommerce')); 
      } 
      */ 
      if($key == 'billing_phone' && $field == ''){ 
       $woocommerce->add_error('<strong>' . __('ERROR', 'woocommerce') . '</strong>: ' . __('Please enter phone number.', 'woocommerce')); 
      } 

     } 
    endforeach; 
} 
add_action('register_post','zk_validation_billing_address'); 
+0

伟大的插件,但验证似乎没有工作,任何想法? – Godge

+0

我以为我会这样做,但事实证明我没有。禁用保存功能似乎使验证工作,但我找不到一种方法,使他们都一起工作:( – Godge