第一步:注册
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我需要保持信息! –