WooCommerce:在结帐、我的帐户、管理订单和 WordPress 用户中添加生日账单字段
WooCommerce: Add a birthdate billing field in checkout, My account, admin orders and WordPress user
我尝试在 Woocommerce Checkout 表单中添加一个生日字段,然后将其保存为用户元数据。
我可以用下面的代码显示它,但我无法保存它以便在用户个人资料页面中看到它。
add_filter( 'woocommerce_billing_fields', 'add_birth_date_billing_field', 20, 1 );
function add_birth_date_billing_field($billing_fields) {
$billing_fields['billing_birth_date'] = array(
'type' => 'date',
'label' => __('Birth date'),
'class' => array('form-row-wide'),
'priority' => 25,
'required' => true,
'clear' => true,
);
return $billing_fields;
}
add_action( 'woocommerce_checkout_update_customer', 'save_checkout_account_birthday_field', 10, 2 );
function save_checkout_account_birthday_field( $customer, $data ){
if ( isset($_POST['billing_birth_date']) && ! empty($_POST['billing_birth_date']) ) {
$customer->update_meta_data( 'billing_birth_date', sanitize_text_field($_POST['billing_birth_date']) );
}
}
这是将在结帐、我的帐户地址、管理订单页面和 WordPress 用户仪表板中显示账单出生日期的完整代码:
// Display Billing birthdate field to checkout and My account addresses
add_filter( 'woocommerce_billing_fields', 'display_birthdate_billing_field', 20, 1 );
function display_birthdate_billing_field($billing_fields) {
$billing_fields['billing_birthdate'] = array(
'type' => 'date',
'label' => __('Birthdate'),
'class' => array('form-row-wide'),
'priority' => 25,
'required' => true,
'clear' => true,
);
return $billing_fields;
}
// Save Billing birthdate field value as user meta data
add_action( 'woocommerce_checkout_update_customer', 'save_account_billing_birthdate_field', 10, 2 );
function save_account_billing_birthdate_field( $customer, $data ){
if ( isset($_POST['billing_birthdate']) && ! empty($_POST['billing_birthdate']) ) {
$customer->update_meta_data( 'billing_birthdate', sanitize_text_field($_POST['billing_birthdate']) );
}
}
// Admin orders Billing birthdate editable field and display
add_filter('woocommerce_admin_billing_fields', 'admin_order_billing_birthdate_editable_field');
function admin_order_billing_birthdate_editable_field( $fields ) {
$fields['birthdate'] = array( 'label' => __('Birthdate', 'woocommerce') );
return $fields;
}
// WordPress User: Add Billing birthdate editable field
add_filter('woocommerce_customer_meta_fields', 'wordpress_user_account_billing_birthdate_field');
function wordpress_user_account_billing_birthdate_field( $fields ) {
$fields['billing']['fields']['billing_birthdate'] = array(
'label' => __('Birthdate', 'woocommerce'),
'description' => __('', 'woocommerce')
);
return $fields;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
我尝试在 Woocommerce Checkout 表单中添加一个生日字段,然后将其保存为用户元数据。 我可以用下面的代码显示它,但我无法保存它以便在用户个人资料页面中看到它。
add_filter( 'woocommerce_billing_fields', 'add_birth_date_billing_field', 20, 1 );
function add_birth_date_billing_field($billing_fields) {
$billing_fields['billing_birth_date'] = array(
'type' => 'date',
'label' => __('Birth date'),
'class' => array('form-row-wide'),
'priority' => 25,
'required' => true,
'clear' => true,
);
return $billing_fields;
}
add_action( 'woocommerce_checkout_update_customer', 'save_checkout_account_birthday_field', 10, 2 );
function save_checkout_account_birthday_field( $customer, $data ){
if ( isset($_POST['billing_birth_date']) && ! empty($_POST['billing_birth_date']) ) {
$customer->update_meta_data( 'billing_birth_date', sanitize_text_field($_POST['billing_birth_date']) );
}
}
这是将在结帐、我的帐户地址、管理订单页面和 WordPress 用户仪表板中显示账单出生日期的完整代码:
// Display Billing birthdate field to checkout and My account addresses
add_filter( 'woocommerce_billing_fields', 'display_birthdate_billing_field', 20, 1 );
function display_birthdate_billing_field($billing_fields) {
$billing_fields['billing_birthdate'] = array(
'type' => 'date',
'label' => __('Birthdate'),
'class' => array('form-row-wide'),
'priority' => 25,
'required' => true,
'clear' => true,
);
return $billing_fields;
}
// Save Billing birthdate field value as user meta data
add_action( 'woocommerce_checkout_update_customer', 'save_account_billing_birthdate_field', 10, 2 );
function save_account_billing_birthdate_field( $customer, $data ){
if ( isset($_POST['billing_birthdate']) && ! empty($_POST['billing_birthdate']) ) {
$customer->update_meta_data( 'billing_birthdate', sanitize_text_field($_POST['billing_birthdate']) );
}
}
// Admin orders Billing birthdate editable field and display
add_filter('woocommerce_admin_billing_fields', 'admin_order_billing_birthdate_editable_field');
function admin_order_billing_birthdate_editable_field( $fields ) {
$fields['birthdate'] = array( 'label' => __('Birthdate', 'woocommerce') );
return $fields;
}
// WordPress User: Add Billing birthdate editable field
add_filter('woocommerce_customer_meta_fields', 'wordpress_user_account_billing_birthdate_field');
function wordpress_user_account_billing_birthdate_field( $fields ) {
$fields['billing']['fields']['billing_birthdate'] = array(
'label' => __('Birthdate', 'woocommerce'),
'description' => __('', 'woocommerce')
);
return $fields;
}
代码进入活动子主题(或活动主题)的 functions.php 文件。已测试并有效。