第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將用戶自定義帳戶字段添加到 WooCommerce 結賬

將用戶自定義帳戶字段添加到 WooCommerce 結賬

PHP
動漫人物 2023-06-24 18:02:34
我使用以下代碼向管理員用戶添加了自定義元字段:``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。另外,我正在訪問 REST API 中的用戶元,當前它在檢查用戶保護程序值后在 REST API 中顯示元,它應該在 wp REST API 中值。我怎樣才能添加這個?
查看完整描述

1 回答

?
拉風的咖菲貓

TA貢獻1995條經(jīng)驗 獲得超2個贊

您可以使用以下命令將user_dob自定義字段添加到結帳帳戶注冊字段:


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 文件中。經(jīng)過測試并有效。

http://img1.sycdn.imooc.com//6496bf570001529605330406.jpg

下訂單后,出生日期將保存到用戶數(shù)據(jù)中并顯示在管理員用戶的“生日”字段中。



查看完整回答
反對 回復 2023-06-24
  • 1 回答
  • 0 關注
  • 156 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號