我想在结帐页面和我的帐户编辑详细信息页面上添加出生日期字段。我在该线程here上遵循了建议的代码,并设法在结帐和管理员编辑用户上获取了自定义字段,但是生日日期字段并未出现在我的帐户编辑详细信息中。怎么了?
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
do_action( 'woocommerce_before_edit_account_form' ); ?>
<form class="woocommerce-EditAccountForm edit-account" action="" method="post">
<?php do_action( 'woocommerce_edit_account_form_start' ); ?>
<p class="woocommerce-FormRow woocommerce-FormRow--first form-row form-row-first">
<label for="account_first_name"><?php _e( 'First name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_first_name" id="account_first_name" value="<?php echo esc_attr( $user->first_name ); ?>" />
</p>
<p class="woocommerce-FormRow woocommerce-FormRow--last form-row form-row-last">
<label for="account_last_name"><?php _e( 'Last name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_last_name" id="account_last_name" value="<?php echo esc_attr( $user->last_name ); ?>" />
</p>
<div class="clear"></div>
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="account_email"><?php _e( 'Email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--email input-text" name="account_email" id="account_email" value="<?php echo esc_attr( $user->user_email ); ?>" />
</p>
<!-- (BEGIN) ADDED: Birth day field -->
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="account_birth_date"><?php _e( 'Birth date', 'theme_domain_slug' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_birth_date" id="account_birth_date" placeholder="dd/mm/yyyy" value="<?php echo esc_attr(get_user_meta( $user->ID, 'account_birth_date', true )); ?>" />
</p>
<div class="clear"></div>
<!-- (END) Birth day field -->
<fieldset>
<legend><?php _e( 'Password Change', 'woocommerce' ); ?></legend>
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="password_current"><?php _e( 'Current Password (leave blank to leave unchanged)', 'woocommerce' ); ?></label>
我希望出生日期字段显示在电子邮件字段之后。
答案 0 :(得分:0)
如果您遇到的情况,我会以不同的方式处理。以下是实现我们目标的一些第一步:
woocommerce
的文件夹(希望您使用的是子主题,请参见此处https://www.hostinger.com/tutorials/how-to-create-wordpress-child-theme)woocommerce
的新myaccount
文件夹内创建另一个文件夹plugins folder -> woocommerce -> templates -> myaccount
并将form-edit-account.php
复制到您在子主题中创建的myaccount
文件夹中现在,您已经覆盖了标准的WooCommerce myaccount页面模板。这是一种安全的方法,因为当您在原始文件中进行更改时,所做的更改将在下一次WooCommerce插件更新后丢失。
现在,您可以打开复制的文件。您会在此处看到myaccount编辑页面的全部内容。
因此所有内容都将如下所示:
<?php
/**
* Edit account form
*
* This template can be overridden by copying it to yourtheme/woocommerce/myaccount/form-edit-account.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce/Templates
* @version 3.5.0
*/
defined( 'ABSPATH' ) || exit;
do_action( 'woocommerce_before_edit_account_form' ); ?>
<form class="woocommerce-EditAccountForm edit-account" action="" method="post" <?php do_action( 'woocommerce_edit_account_form_tag' ); ?> >
<?php do_action( 'woocommerce_edit_account_form_start' ); ?>
<p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first">
<label for="account_first_name"><?php esc_html_e( 'First name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_first_name" id="account_first_name" autocomplete="given-name" value="<?php echo esc_attr( $user->first_name ); ?>" />
</p>
<p class="woocommerce-form-row woocommerce-form-row--last form-row form-row-last">
<label for="account_last_name"><?php esc_html_e( 'Last name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_last_name" id="account_last_name" autocomplete="family-name" value="<?php echo esc_attr( $user->last_name ); ?>" />
</p>
<div class="clear"></div>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="account_display_name"><?php esc_html_e( 'Display name', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_display_name" id="account_display_name" value="<?php echo esc_attr( $user->display_name ); ?>" /> <span><em><?php esc_html_e( 'This will be how your name will be displayed in the account section and in reviews', 'woocommerce' ); ?></em></span>
</p>
<div class="clear"></div>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="account_email"><?php esc_html_e( 'Email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--email input-text" name="account_email" id="account_email" autocomplete="email" value="<?php echo esc_attr( $user->user_email ); ?>" />
</p>
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="account_birth_date"><?php _e( 'Birth date', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_birth_date" id="account_birth_date" placeholder="dd/mm/yyyy" value="<?php echo esc_attr(get_user_meta( $user->ID, 'account_birth_date', true )); ?>" />
</p>
<fieldset>
<legend><?php esc_html_e( 'Password change', 'woocommerce' ); ?></legend>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="password_current"><?php esc_html_e( 'Current password (leave blank to leave unchanged)', 'woocommerce' ); ?></label>
<input type="password" class="woocommerce-Input woocommerce-Input--password input-text" name="password_current" id="password_current" autocomplete="off" />
</p>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="password_1"><?php esc_html_e( 'New password (leave blank to leave unchanged)', 'woocommerce' ); ?></label>
<input type="password" class="woocommerce-Input woocommerce-Input--password input-text" name="password_1" id="password_1" autocomplete="off" />
</p>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="password_2"><?php esc_html_e( 'Confirm new password', 'woocommerce' ); ?></label>
<input type="password" class="woocommerce-Input woocommerce-Input--password input-text" name="password_2" id="password_2" autocomplete="off" />
</p>
</fieldset>
<div class="clear"></div>
<?php do_action( 'woocommerce_edit_account_form' ); ?>
<p>
<?php wp_nonce_field( 'save_account_details', 'save-account-details-nonce' ); ?>
<button type="submit" class="woocommerce-Button button" name="save_account_details" value="<?php esc_attr_e( 'Save changes', 'woocommerce' ); ?>"><?php esc_html_e( 'Save changes', 'woocommerce' ); ?></button>
<input type="hidden" name="action" value="save_account_details" />
</p>
<?php do_action( 'woocommerce_edit_account_form_end' ); ?>
</form>
<?php do_action( 'woocommerce_after_edit_account_form' ); ?>
现在这部分完成了,您可以再次上传文件。但是您还需要另一部分,因为当用户按下“保存”按钮时,没有此功能将无法保存更改:
/**
* Save user account settings
*/
add_action( 'woocommerce_save_account_details', 'save_user_account_settings', 12, 1 );
function save_user_account_settings( $user_id ) {
//Save the account birth date when the user submits the account settings form
if ( isset( $_POST['account_birth_date'] ) ) {
update_user_meta( $user_id, 'account_birth_date', sanitize_text_field( $_POST['account_birth_date'] ) );
}
}
这部分进入您的子主题functions.php
文件。现在所有人都应该像应该的那样工作。请告诉我它是否有效,并检查调试日志中是否有错误。因为当出现错误时,您的网站会变得越来越慢(请记住)。