2017-04-11 95 views
0

我想增值稅字段添加到客戶賬單地址,而這個工程的結帳頁面上用下面的代碼:WooCommerce客戶帳單地址

// Company Name Required 
add_filter('woocommerce_checkout_fields','custom_override_checkout_fields'); 
function custom_override_checkout_fields($fields){ 
    $fields['billing']['billing_company']['required'] = true; 
    $fields['billing']['billing_vat'] = array(
    'label'  => __('VAT Number','woocommerce'), 
    'placeholder' => _x('Enter VAT Number','placeholder','woocommerce'), 
    'required' => true, 
    'class'  => array('form-row-wide'), 
    'clear'  => true 
    ); 
    return $fields; 
} 
//Display field value on the order edit page 
add_action('woocommerce_admin_order_data_after_shipping_address','my_custom_checkout_field_display_admin_order_meta',10,1); 
function my_custom_checkout_field_display_admin_order_meta($order){ 
    echo '<p><strong>'.__('VAT Number').':</strong> ' . get_post_meta($order->id,'_billing_vat',true) . '</p>'; 
} 
//Order the fields 
add_filter("woocommerce_checkout_fields","order_fields"); 
function order_fields($fields){ 
    $order = array(
     "billing_first_name", 
     "billing_last_name", 
     "billing_company", 
     "billing_vat", 
     "billing_country", 
     "billing_city", 
     "billing_postcode", 
     "billing_state", 
     "billing_address_1", 
     "billing_address_2", 
     "billing_email", 
     "billing_phone", 
    ); 
foreach($order as $field){$ordered_fields[$field] = $fields["billing"][$field];} 
$fields["billing"] = $ordered_fields; 
return $fields; 
} 

我還需要它要在客戶設定賬戶選項中的賬單地址。由於我需要將其鏈接到註冊頁面,因爲我希望用戶使用他們的所有憑證進行註冊,包括他們爲B2B網上商店擁有的VAT號碼。

是否有人知道或是否有人能指出我正確的方向?我將如何執行此任務,不僅在結賬頁面上顯示增值稅號碼的結算字段,還會在用戶個人資料頁面上顯示,以及如何在註冊頁面添加所有這些字段?

在此先感謝您對本案的任何幫助!

回答

1

嗯,這很簡單。您的代碼應該是這樣的:

/* ---------------------- Registration page ----------------------- */ 

/* Add extra fields in registration form */ 
add_action('woocommerce_register_form_start', 'my_extra_register_fields'); 
function my_extra_register_fields() { 
?> 
    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide"> 
     <label for="reg_billing_vat"><?php _e('Billing VAT', 'woocommerce'); ?><span class="required">*</span></label> 
     <input type="text" class="input-text" name="billing_vat" id="reg_billing_vat" value="<?php if (! empty($_POST['billing_vat'])) esc_attr_e($_POST['billing_vat']); ?>"> 
    </p> 
    <div class="clearfix"></div> 
<?php 
} 

/* registration form fields Validation */ 
add_action('woocommerce_register_post', 'my_validate_extra_register_fields', 10, 3); 
function my_validate_extra_register_fields($username, $email, $validation_errors) { 

    if (isset($_POST['billing_vat']) && empty($_POST['billing_vat'])) { 
     $validation_errors->add('billing_vat_error', __('VAT number is required!', 'woocommerce')); 
    } 

    return $validation_errors; 
} 

/* Below code save extra fields when new user register */ 
add_action('woocommerce_created_customer', 'my_save_extra_register_fields'); 
function my_save_extra_register_fields($customer_id) { 

    if (isset($_POST['billing_vat'])) {  

     // VAT field which is used in WooCommerce 
     update_user_meta($customer_id, 'billing_vat', sanitize_text_field($_POST['billing_first_name'])); 
    } 

} 


/* ---------------------- Account page ----------------------- */ 

/* Show custom fields on Account details page */ 
add_action('woocommerce_edit_account_form', 'my_woocommerce_edit_account_form'); 
function my_woocommerce_edit_account_form() { 
    $user_id = get_current_user_id(); 
    $user = get_userdata($user_id); 

    if (!$user) return; 

    $billing_vat = get_user_meta($user_id, 'billing_vat', true); 
?> 
    <fieldset> 
     <legend>Custom information</legend> 

     <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide"> 
      <label for="billing_vat">Billing VAT</label> 
      <input type="text" name="billing_vat" id="billing_vat" value="<?php echo esc_attr($billing_vat); ?>" class="input-text" /> 
     </p> 
     <div class="clearfix"></div> 

    </fieldset> 
    <?php 
} 

/* Below code save extra fields when account details page form submitted */ 
add_action('woocommerce_save_account_details', 'my_woocommerce_save_account_details'); 
function my_woocommerce_save_account_details($user_id) { 

    if (isset($_POST['billing_vat'])) { 
     update_user_meta($user_id, 'billing_vat', sanitize_text_field($_POST['billing_vat'])); 
    } 

} 

您可以根據需要添加更多自定義字段。

是的,您可以使用woocommerce_billing_fields過濾器鉤子在my-account/edit-address/billing/下添加自定義字段。

因此,對於這個代碼,應該有如下:

/* Add field under my account billing */ 
add_filter('woocommerce_billing_fields', 'my_woocommerce_billing_fields'); 
function my_woocommerce_billing_fields($fields) { 

    $user_id = get_current_user_id(); 
    $user = get_userdata($user_id); 

    if (!$user) return; 

    $fields['billing_vat'] = array(
     'type'  => 'text', 
     'label'  => __('VAT', 'woocommerce'), 
     'placeholder' => _x('VAT Number', 'placeholder', 'woocommerce'), 
     'required' => true, 
     'class'  => array('form-row'), 
     'clear'  => true, 
     'default' => get_user_meta($user_id, 'billing_vat', true) // assing default value if any 
    ); 

    return $fields; 
} 

/* Format custom field to show on my account billing */ 
add_filter('woocommerce_my_account_my_address_formatted_address', 'custom_my_account_my_address_formatted_address', 10, 3); 
function custom_my_account_my_address_formatted_address($fields, $customer_id, $name) { 

    $fields['vat'] = get_user_meta($customer_id, $name . '_vat', true); 

    return $fields; 
} 

/* Replace the key for custom field to show on my account billing */ 
add_filter('woocommerce_formatted_address_replacements', 'custom_formatted_address_replacements', 10, 2); 
function custom_formatted_address_replacements($address, $args) { 
    $address['{vat}'] = ''; 

    if (! empty($args['vat'])) { 
     $address['{vat}'] = __('VAT Number', 'woocommerce') . ': ' . $args['vat']; 
    } 

    return $address; 
} 
add_filter('woocommerce_localisation_address_formats', 'custom_localisation_address_format'); 
function custom_localisation_address_format($formats) { 

    foreach($formats as $key => $value) : 
     $formats[$key] .= "\n\n{vat}"; 
    endforeach; 

    return $formats; 
} 
+0

爲輔助真棒的感謝!我註冊額外的領域似乎正在工作,我現在也瞭解如何添加更多的註冊頁面。 但是我注意到增值稅字段出現在賬戶詳細信息下,但是我希望在賬單地址(我的賬戶/編輯地址/賬單/)下有增值稅字段。 我可能可以通過不同地址來改變這個鉤子嗎?像「woocommerce_after_edit_account_address_form」一樣,我想知道是否有方法來排列元素。 無論哪種方式,我都非常感謝您爲幫助我而付出的努力! –

+0

不客氣!我編輯了我的答案,在帳單地址(我的帳戶/編輯地址/結算/)下添加自定義字段。請讓我知道這對你有沒有用。 :-) –

+0

標記爲已接受的答案,並感謝一百萬人!大力支持!保持我想說的奇妙的工作,很高興看到這個世界上仍然有很棒的人! –

相關問題