将用户自定义帐户字段添加到 WooCommerce 结帐 [英] Add user custom account field to WooCommerce checkout

查看:49
本文介绍了将用户自定义帐户字段添加到 WooCommerce 结帐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码向管理员用户添加了一个自定义元字段:``

I have added to admin users a custom meta field using the following code:``

function wporg_usermeta_form_field_birthday( $user )
{
    ?>
    
    <table class="form-table" id="table-form-dob" >
        <tr>
            <th><h3 style="margin: 0">Extra Meta Fields</h3></th>
        </tr>
        <tr>
            <th>
                <label for="user_dob">Birthday</label>
            </th>
            <td>
                <input type="date"
                       class="regular-text ltr"
                       id="user_dob"
                       name="user_dob"
                       value="<?= esc_attr( get_user_meta( $user->ID, 'user_dob', true ) ) ?>"
                       title="Please use YYYY-MM-DD as the date format."
                       pattern="(19[0-9][0-9]|20[0-9][0-9])-(1[0-2]|0[1-9])-(3[01]|[21][0-9]|0[1-9])"
                       required>
                
            </td>
        </tr>
    </table>
    <script>
        jQuery(function($){
        jQuery('#table-form-dob tr').insertAfter(jQuery('#display_name').parentsUntil('tr').parent());
    });
    </script>
    <?php
}
  
function wporg_usermeta_form_field_birthday_update( $user_id )
{
    if ( ! current_user_can( 'edit_user', $user_id ) ) {
        return false;
    }
    return update_user_meta(
        $user_id,
        'user_dob',
        $_POST['user_dob']
    );
}
  
add_action(
    'show_user_profile',
    'wporg_usermeta_form_field_birthday'
);
  
add_action(
    'edit_user_profile',
    'wporg_usermeta_form_field_birthday'
);
  
add_action(
    'personal_options_update',
    'wporg_usermeta_form_field_birthday_update'
);
  
add_action(
    'edit_user_profile_update',
    'wporg_usermeta_form_field_birthday_update'
);

register_meta('user', 'user_dob', array(
  "type" => "string",
  "show_in_rest" => true // this is the key part
));

我想在woocommerce结账页面添加这个相同的字段,所以当用户在woocommerce结账页面注册时,我们应该能够看到这个生日"字段 (user_dob) 在管理员用户配置文件/编辑部分.

I want to add this same field In woocommerce checkout page, so when the user get registered in woocommerce checkout page, we should be able to see this "Birthday" field (user_dob) in admin user profile/ edit section.

此外,我正在访问 rest API 中的用户元数据,当前它在检查用户保护程序值后在 rest API 中显示元数据,它应该在 wp rest API 中取值.

also, I am accessing user meta in rest API currently its showing meta in rest API after user saver values in check it should value in wp rest API.

如何添加?

推荐答案

您可以使用以下内容将 user_dob 自定义字段添加到结帐帐户注册字段:

You can use the following that will add user_dob custom field to checkout account registration fields:

add_filter( 'woocommerce_checkout_fields', 'add_checkout_account_birthday_field' );
function add_checkout_account_birthday_field( $fields ){
    $fields['account']['user_dob'] = array(
        'type'              => 'date',
        'label'             => __("Birthday", "woocommerce"),
        'placeholder'       => __("Please use YYYY-MM-DD as the date format.", "woocommerce"),
        'class'             => array('form-row-wide regular-text ltr'),
        'required'          => true,
        'custom_attributes' => ['pattern' => '(19[0-9][0-9]|20[0-9][0-9])-(1[0-2]|0[1-9])-(3[01]|[21][0-9]|0[1-9])'],
    );
    return $fields;
}

add_action( 'woocommerce_checkout_update_customer', 'save_checkout_account_birthday_field', 10, 2 );
function save_checkout_account_birthday_field( $customer, $data ){
    if ( isset($_POST['user_dob']) && ! empty($_POST['user_dob']) ) {
         $customer->update_meta_data( 'user_dob', sanitize_text_field($_POST['user_dob']) );
    }
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

一旦下订单,出生日期就会保存到用户数据中并显示在管理员用户生日"中.领域.

Once order is placed the birth date is saved to user data and displayed in admin user "Birthday" field.

这篇关于将用户自定义帐户字段添加到 WooCommerce 结帐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆