我使用钩子添加了Birthday作为自定义用户元数据
我可以添加/查看以下信息:
1.用户〉配置文件部分
1.我的账户页面
1.结账页面
自定义元数据:$birthday = $current_user->user_birthdate;
我在整个数据库中寻找,“user_birthdate”,包括,“wp_users”,“wp_usermeta”,wp_postmeta”**表,但在任何地方都找不到它。
我需要弄清楚在WordPress数据库中的何处查找自定义用户元数据值
- PS:我将数据库导出为.csv并使用Excel处理数据 *
工作代码: - 添加到functions.php *
<?php
// Display DOB on checkout page
add_action( 'woocommerce_before_checkout_billing_form', 'func_before_checkout_form', 10 );
function func_before_checkout_form( $checkout ) {
echo '<h3>Checkout Form</h3>';
$current_user = wp_get_current_user();
$birthday = $current_user->user_birthdate;
woocommerce_form_field( 'user_birthdate', array(
'type' => 'date',
'class' => array('form-row-wide'),
'label' => 'Birthday',
'required' => true,
'default' => $birthday,
), $checkout->get_value( 'user_birthdate' ) );
}
// Validate New Checkout Field.
add_action( 'woocommerce_checkout_process', 'validate_user_birthdate_checkout_field' );
function validate_user_birthdate_checkout_field() {
if ( ! $_POST['user_birthdate'] ) {
wc_add_notice( 'Please enter your Birthdate', 'error' );
}
//check age
if( isset($_POST['user_birthdate']) && ! empty($_POST['user_birthdate']) ){
// Get customer age from birthdate
$age = date_diff(date_create($_POST['user_birthdate']), date_create('now'))->y;
// Checking age and display an error notice avoiding checkout (and emptying cart)
if($age<18){
wc_add_notice( __( "You need at least to be 18 years old, to be able to checkout." ), "error" );
//WC()->cart->empty_cart(); // <== Empty cart (optional)
}
}
}
// 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['user_birthdate']) && ! empty($_POST['user_birthdate']) ) {
$customer->update_meta_data( 'user_birthdate', sanitize_text_field($_POST['user_birthdate']) );
}
}
// Add field - admin.
function add_user_birtday_field( $user ) {
?>
<h3><?php _e('Birthday','woocommerce' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="user_birthdate"><?php _e( 'Date of Birth', 'woocommerce' ); ?></label></th>
<td><input type="date" name="user_birthdate" value="<?php echo esc_attr( get_the_author_meta( 'user_birthdate', $user->ID )); ?>" class="regular-text" /></td>
</tr>
</table>
<br />
<?php
}
add_action( 'show_user_profile', 'add_user_birtday_field', 10, 1 );
add_action( 'edit_user_profile', 'add_user_birtday_field', 10, 1 );
// Save field - admin section
function save_user_birtday_field( $user_id ) {
if( ! empty($_POST['user_birthdate']) ) {
update_user_meta( $user_id, 'user_birthdate', sanitize_text_field( $_POST['user_birthdate'] ) );
}
}
add_action( 'personal_options_update', 'save_user_birtday_field', 10, 1 );
add_action( 'edit_user_profile_update', 'save_user_birtday_field', 10, 1 );
// Add field - my account page.
function action_woocommerce_edit_account_form() {
woocommerce_form_field( 'user_birthdate', array(
'type' => 'date',
'label' => __( 'Birth Date', 'woocommerce' ),
'description' => __('', 'woocommerce'),
'placeholder' => __( 'Date of Birth', 'woocommerce' ),
), get_user_meta( get_current_user_id(), 'user_birthdate', true ));
}
add_action( 'woocommerce_edit_account_form', 'action_woocommerce_edit_account_form' );
// Validate - my account page.
function action_woocommerce_save_account_details_errors( $args ){
if ( isset($_POST['user_birthdate']) && empty($_POST['user_birthdate']) ) {
$args->add( 'error', __( 'Please provide a birth date', 'woocommerce' ) );
}
//check age
if( isset($_POST['user_birthdate']) && ! empty($_POST['user_birthdate']) ){
// Get customer age from birthdate
$age = date_diff(date_create($_POST['user_birthdate']), date_create('now'))->y;
// Checking age and display an error notice avoiding checkout (and emptying cart)
if( $age < 18 ){
wc_add_notice( __( "You need at least to be 18 years old, to be able to checkout." ), "error" );
//WC()->cart->empty_cart(); // <== Empty cart (optional)
}
}
}
add_action( 'woocommerce_save_account_details_errors','action_woocommerce_save_account_details_errors', 10, 1 );
// Save - my account page.
function action_woocommerce_save_account_details( $user_id ) {
if( isset($_POST['user_birthdate']) && ! empty($_POST['user_birthdate']) ) {
update_user_meta( $user_id, 'user_birthdate', sanitize_text_field($_POST['user_birthdate']) );
}
}
add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
- PS:我不太擅长PHP*
1条答案
按热度按时间yhxst69z1#
它保留在wp_usermeta表中。但是,它不是该表的列。这是一个 meta_key =“user_birthdate”的记录。试着运行下面的查询,你会看到它: