第一步:註冊
1時),您需要更改WooCommerce設置登記預約首次設置帳單地址。
WooCommerce 設置>帳戶(製表符):
2)添加帳單字段中的用戶註冊我的賬戶頁:
基地:How to add custom fields in user registration on the "My Account" page
定製代碼(複製代碼在您的活動子主題或主題的function.php文件):
if(!is_admin())
{
// 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 my_custom_function()
{
global $woocommerce;
$checkout = $woocommerce->checkout();
?>
<h3><?php _e('Billing Address', 'woocommerce'); ?></h3>
<?php
foreach ($checkout->checkout_fields['billing'] as $key => $field) :
woocommerce_form_field($key, $field, $checkout->get_value($key));
endforeach;
}
add_action('register_form','my_custom_function');
// Custom function to save Usermeta or Billing Address of registered user
function save_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]);
}
endforeach;
}
add_action('woocommerce_created_customer','save_address');
// Registration page billing address form Validation
function custom_validation()
{
global $woocommerce;
$address = $_POST;
foreach ($address as $key => $field) :
// Validation: Required fields
if(startsWith($key,'billing_'))
{
if($key == 'billing_country' && $field == '')
{
$woocommerce->add_error('' . __('ERROR', 'woocommerce') . ': ' . __('Please select a country.', 'woocommerce'));
}
if($key == 'billing_first_name' && $field == '')
{
$woocommerce->add_error('' . __('ERROR', 'woocommerce') . ': ' . __('Please enter first name.', 'woocommerce'));
}
if($key == 'billing_last_name' && $field == '')
{
$woocommerce->add_error('' . __('ERROR', 'woocommerce') . ': ' . __('Please enter last name.', 'woocommerce'));
}
if($key == 'billing_address_1' && $field == '')
{
$woocommerce->add_error('' . __('ERROR', 'woocommerce') . ': ' . __('Please enter address.', 'woocommerce'));
}
if($key == 'billing_city' && $field == '')
{
$woocommerce->add_error('' . __('ERROR', 'woocommerce') . ': ' . __('Please enter city.', 'woocommerce'));
}
if($key == 'billing_state' && $field == '')
{
$woocommerce->add_error('' . __('ERROR', 'woocommerce') . ': ' . __('Please enter state.', 'woocommerce'));
}
if($key == 'billing_postcode' && $field == '')
{
$woocommerce->add_error('' . __('ERROR', 'woocommerce') . ': ' . __('Please enter a postcode.', 'woocommerce'));
}
if($key == 'billing_email' && $field == '')
{
$woocommerce->add_error('' . __('ERROR', 'woocommerce') . ': ' . __('Please enter billing email address.', 'woocommerce'));
}
if($key == 'billing_phone' && $field == '')
{
$woocommerce->add_error('' . __('ERROR', 'woocommerce') . ': ' . __('Please enter phone number.', 'woocommerce'));
}
}
endforeach;
}
add_action('register_post','custom_validation');
}
第二步:Hidding從結賬白化帳單地址刪除
您需要以display: none !important;
規則使用針對結算區塊字段的CSS。
非常感謝這個明確的答案!但我想要的是不在結帳頁面再次詢問帳單地址,我添加了一些代碼以將其在function.php文件中刪除,但是當它註冊爲空時它不會獲得地址集! –
@AbdelazizMirad因此,您可能需要禁用woocommerce設置中的複選框**在「結帳」頁面上顯示返回客戶登錄提醒**。你能提供一個鏈接到你的網站?在結帳時隱藏結算字段最好使用CSS規則,正如我所說的那樣。 – LoicTheAztec
我試了一下在結帳和我的帳戶選項卡,是啊沒有問題,我可以隱藏HTML/CSS我需要保持信息! –