简体   繁体   English

在 Woocommerce 编辑帐户页面中添加自定义字段

[英]Add a custom field in Woocommerce Edit Account page

I have custom checkout fields favorite_color that users can fill in during checkout (not required) ,imilar to other default checkout fields...我有自定义结帐字段favorite_color用户可以在结帐时填写(不是必需的) ,类似于其他默认结帐字段...

In My Account section in "Edit Account", I Would like to add favorite_color custom field to allow customer editing this field value.在“编辑帐户”的“我的帐户”部分中,我想添加favorite_color自定义字段以允许客户编辑此字段值。

For that I have edited the template myaccount/form-edit-account.php by copying and editing an original line of code in it:为此,我通过复制和编辑其中的原始代码行来编辑模板myaccount/form-edit-account.php

    <p class="woocommerce-form-row woocommerce-form-row--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>

I replaced the account_first_name with my favorite_color and the field even shows the saved value in the field (thanks to $user->favorite_color )我用我的收藏夹颜色替换了account_first_name并且该字段甚至显示了该字段中保存的值(感谢$user->favorite_color

But the bad part is, it does not get saved.但不好的部分是,它没有得救。

What code must I add to get this working and getting saved?我必须添加什么代码才能使其正常工作并得到保存?

The original template code is:原模板代码为:

<?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/
 * @author  WooThemes
 * @package WooCommerce/Templates
 * @version 2.6.0
 */

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-form-row woocommerce-form-row--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-form-row woocommerce-form-row--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-form-row woocommerce-form-row--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>

    <fieldset>
        <legend><?php _e( 'Password change', 'woocommerce' ); ?></legend>

        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
            <label for="password_current"><?php _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" />
        </p>
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
            <label for="password_1"><?php _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" />
        </p>
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
            <label for="password_2"><?php _e( 'Confirm new password', 'woocommerce' ); ?></label>
            <input type="password" class="woocommerce-Input woocommerce-Input--password input-text" name="password_2" id="password_2" />
        </p>
    </fieldset>
    <div class="clear"></div>

    <?php do_action( 'woocommerce_edit_account_form' ); ?>

    <p>
        <?php wp_nonce_field( 'save_account_details' ); ?>
        <input type="submit" class="woocommerce-Button button" name="save_account_details" value="<?php esc_attr_e( 'Save changes', 'woocommerce' ); ?>" />
        <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' ); ?>

This can be done without overriding template files, just using available hooks this way:这可以在不覆盖模板文件的情况下完成,只需以这种方式使用可用的钩子:

// Add the custom field "favorite_color"
add_action( 'woocommerce_edit_account_form', 'add_favorite_color_to_edit_account_form' );
function add_favorite_color_to_edit_account_form() {
    $user = wp_get_current_user();
    ?>
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <label for="favorite_color"><?php _e( 'Favorite color', 'woocommerce' ); ?></label>
        <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="favorite_color" id="favorite_color" value="<?php echo esc_attr( $user->favorite_color ); ?>" />
    </p>
    <?php
}

// Save the custom field 'favorite_color' 
add_action( 'woocommerce_save_account_details', 'save_favorite_color_account_details', 12, 1 );
function save_favorite_color_account_details( $user_id ) {
    // For Favorite color
    if( isset( $_POST['favorite_color'] ) )
        update_user_meta( $user_id, 'favorite_color', sanitize_text_field( $_POST['favorite_color'] ) );

    // For Billing email (added related to your comment)
    if( isset( $_POST['account_email'] ) )
        update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['account_email'] ) );
}

Code goes in function.php file of your active child theme (or active theme) or in any plugin file.代码位于活动子主题(或活动主题)的 function.php 文件或任何插件文件中。

Tested and works.测试和工作。

If you want to override the template to display the custom field, you just have to keep the 2nd hooked function (the one who saves the data when edited).如果要覆盖模板以显示自定义字段,则只需保留第二个挂钩函数(编辑时保存数据的函数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Woocommerce 编辑帐户页面中添加额外的自定义字段 - Adding an additional custom field in Woocommerce Edit Account page 如何将年龄字段添加到woocommerce编辑帐户 - How to add age field to woocommerce Edit account 在我的帐户上添加手机字段 &gt; 在 Woocommerce 中编辑帐户 - Add a mobile phone field on My account > edit account in Woocommerce 将用户自定义帐户字段添加到 WooCommerce 结帐 - Add user custom account field to WooCommerce checkout WooCommerce 订单编辑页面自定义字段 - WooCommerce custom field in Orders edit page WooCommerce新账户页面添加自定义字段 - Add custom fields to WooCommerce new account Page 将高级自定义字段添加到 Dokan 上的现有表单 - 添加产品和编辑产品页面 (WordPress/WooCommerce) - Add Advanced Custom Field to existing form on Dokan- Add product & edit product page (WordPress/WooCommerce) WooCommerce:在“我的帐户”的编辑页面上验证自定义字段 - WooCommerce: Validate custom fields on My Account's edit page Woocommerce:验证我的帐户编辑页面上的自定义字段 - Woocommerce: validate custom fields on my account edit page 如何在 WooCommerce “我的帐户”页面上对自定义字段重新排序或添加优先级 - How do I re-order or add priority to a custom field on WooCommerce “My Account” page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM