如何将性别字段添加到 woocommerse 编辑帐户 [英] How to add Gender field to woocommerse Edit account
问题描述
我在 woocommerse 商店网站上工作,需要在用户个人资料页面中显示性别字段.我试图将以下代码添加到functions.php
I am working on a woocommerse shop website and need to display the gender field within user profile page. I have tried to add the following code to functions.php
add_action( 'show_user_profile', 'show_extra_profile_fields' );
add_action( 'edit_user_profile', 'show_extra_profile_fields' );
function show_extra_profile_fields( $user ) { ?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="gender">Gender</label></th>
<td>
}
<select name="gender" id="gender" >
<option value="Male" <?php selected( 'Male', get_the_author_meta( 'gender', $user->ID ) ); ?>>Male</option>
<option value="Female" <?php selected( 'Female', get_the_author_meta( 'gender', $user->ID ) ); ?>>Female</option>
</select>
</td>
</tr>
</table>
<?php }
add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
function save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_usermeta( $user_id, 'gender', $_POST['gender'] );
}
它在 Wordpress 用户个人资料页面中显示性别字段,但不在 woocommerce 用户个人资料页面中显示.
it is displaying the gender field within Wordpress user profile page but not within woocommerce user profile page.
可以吗?
提前致谢.
推荐答案
在您的自定义 form-edit-account.php
(您从 woocommerce/templates/nyaccount
) 文件:
Add this after the email field in your custom form-edit-account.php
(the one you copied from woocommerce/templates/nyaccount
) file:
<p class="form-row form-row-last">
<label for="gender">Gender:</label>
<select name="gender" id="gender">
<option value="Male" <?php selected('Male', get_user_meta($user->ID, 'gender', true)); ?>>Male</option>
<option value="Female" <?php selected('Female', get_user_meta($user->ID, 'gender', true)); ?>>Female</option>
</select>
</p>
<div class="clear"></div>
将此添加到您主题的functions.php
文件中:
Add this to your theme's functions.php
file:
// ---- Save user input into database.
add_action('woocommerce_save_account_details', 'custom_woocommerce_save_account_details');
function custom_woocommerce_save_account_details($user_id) {
if ($user_id) {
if (isset($_POST['gender'])) {
if ($_POST['gender'] == 'Male' || $_POST['gender'] == 'Female') {
update_user_meta($user_id, 'gender', $_POST['gender']);
}
}
}
}
那肯定行得通.
这篇关于如何将性别字段添加到 woocommerse 编辑帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!