简体   繁体   English

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

[英]Adding an additional custom field in Woocommerce Edit Account page

IN WooCommerce, I have been able to add a custom fields in Edit Account page.在 WooCommerce 中,我已经能够在“编辑帐户”页面中添加自定义字段。 I have tried adding a 2nd custom field "Favorite Color 2" but I ca't get it working, There is something that I am doing wrong.我尝试添加第二个自定义字段“Favorite Color 2”,但无法正常工作,我做错了什么。

How I can make to add/save an additional custom field in Edit Account page?我如何才能在“编辑帐户”页面中添加/保存额外的自定义字段?

// 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' ); ?>
        <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'] ) );

This can be done very easily Making some changes in your code 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();

    // First Field
    ?>
    <p class="woocommerce-form-row woocommerce-form-row--first form-row form-row-first">
        <label for="favorite_color"><?php _e( 'Favorite color', 'woocommerce' ); ?>
        <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
    // Second Field
    ?>
    <p class="woocommerce-form-row woocommerce-form-row--last form-row form-row-last">
        <label for="favorite_color"><?php _e( 'Favorite color 2', 'woocommerce' ); ?>
        <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="favorite_color2" id="favorite_color2" value="<?php echo esc_attr( $user->favorite_color2 ); ?>" />
    </p>
    <div class="clear"></div>
    <?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 Favorite color 2
    if( isset( $_POST['favorite_color2'] ) )
        update_user_meta( $user_id, 'favorite_color2', sanitize_text_field( $_POST['favorite_color2'] ) );

    // 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.测试和工作。

You will get this:你会得到这个:

在此处输入图片说明

Use woocommerce_edit_account_form hook as the below example使用 woocommerce_edit_account_form 挂钩作为以下示例

 add_action( 'woocommerce_edit_account_form', 'cssigniter_add_account_details' );
    function cssigniter_add_account_details() {
        woocommerce_form_field(
            'billing_company',
            array(
                'type'        => 'text',
                'required'    => true, // remember, this doesn't make the field required, just adds an "*"
                'label'       => 'Company Name'
            ),
            get_user_meta( get_current_user_id(), 'billing_company', true ) // get the data
        );
        woocommerce_form_field(
            'company_id',
            array(
                'type'        => 'number',
                'required'    => true, // remember, this doesn't make the field required, just adds an "*"
                'label'       => 'Company ID'
            ),
            get_user_meta( get_current_user_id(), 'company_id', true ) // get the data
        );
        
    
    }
    // Save field value
    add_action( 'woocommerce_save_account_details', 'misha_save_account_details' );
    function misha_save_account_details( $user_id ) {
        
        update_user_meta( $user_id, 'billing_company', wc_clean( $_POST[ 'phone_number' ] ) );
            update_user_meta( $user_id, 'company_id', wc_clean( $_POST[ 'phone_number' ] ) );
    
    }
    // Make it required
    add_filter( 'woocommerce_save_account_details_required_fields', 'misha_make_field_required' );
    function misha_make_field_required( $required_fields ){
        
        $required_fields[ 'billing_company' ] = 'Company Name';
            $required_fields[ 'company_id' ] = 'Company ID';
    
        return $required_fields;
    
    }

Add the above code to you theme functions.php file.将以上代码添加到主题 functions.php 文件中。

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

相关问题 在 Woocommerce 编辑帐户页面中添加自定义字段 - Add a custom field in Woocommerce Edit Account page WooCommerce 订单编辑页面自定义字段 - WooCommerce custom field in Orders edit page Woocommerce 附加信息选项卡:添加产品自定义字段值 - Woocommerce Additional Information Tab: Adding product custom field value WooCommerce:在“我的帐户”的编辑页面上验证自定义字段 - WooCommerce: Validate custom fields on My Account's edit page Woocommerce:验证我的帐户编辑页面上的自定义字段 - Woocommerce: validate custom fields on my account edit page 仅允许在我的帐户上编辑每个客户一次自定义字段 &gt; 在 WooCommerce 中编辑帐户 - Only allow edit of custom field once per customer on My account > edit account in WooCommerce 在WooCommerce产品选项编辑页面中,在SKU之前显示自定义字段 - In WooCommerce product options edit page display a custom field before the SKU 在订单编辑页面(后端)隐藏自定义字段 label,如果在 WooCommerce 中为空 - Hide custom field label on the order edit page (backend), if it is empty in WooCommerce Woocommerce function 在订单编辑页面显示此自定义字段值 - Woocommerce function to display this custom field value on the order edit page 自定义出生日期字段未显示在我的帐户编辑详细信息页面中 - Custom Birth Date field not showing in my account edit details page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM