【问题标题】:How to add age field to woocommerce Edit account如何将年龄字段添加到 woocommerce 编辑帐户
【发布时间】:2023-03-11 18:58:01
【问题描述】:

我正在开发 woocommerce 商店网站,需要在用户个人资料页面中显示年龄字段。 我发现这是关于性别的,并询问是否有类似的年龄

How to add Gender field to woocommerse Edit account

【问题讨论】:

  • 请尝试更新的代码,它应该可以正常工作。

标签: php wordpress woocommerce field account


【解决方案1】:

更新 2

首先查看this official documentation,了解如何通过活动子主题(或活动它们)覆盖 Woocommerce 模板

要将年龄字段添加到“我的帐户”>“编辑帐户”部分,请打开/编辑 myaccount/form-edit-account.php 模板文件。

从第 36 行到第 40 行替换以下代码块(包括“年龄”字段)

<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' ); ?>&nbsp;<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--first form-row form-row-first">
    <label for="account_age"><?php esc_html_e( 'Age', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
    <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="account_age" id="account_age" value="<?php echo isset($user->age) ? esc_attr( $user->age ) : ''; ?>" />
</p>
<p class="woocommerce-form-row woocommerce-form-row--last form-row form-row-last">
    <label for="account_display_name"><?php esc_html_e( 'Display name', 'woocommerce' ); ?>&nbsp;<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>

将年龄字段值保存在数据库中:

add_action('woocommerce_save_account_details', 'save_account_details_age', 20, 1 );
function save_account_details_age( $user_id ) {
    if ( isset($_POST['account_age']) ) {
        update_user_meta($user_id, 'age', sanitize_text_field($_POST['account_age'] ) );
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

【讨论】:

    猜你喜欢
    • 2018-12-08
    • 2022-01-03
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 2021-05-30
    • 1970-01-01
    相关资源
    最近更新 更多