亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

將用戶角色作為自定義元數據添加到WooCommerce訂單

將用戶角色作為自定義元數據添加到WooCommerce訂單

PHP
慕妹3146593 2021-04-29 13:14:32
在WooCommerce中,我想知道是否可以使用元鍵將用戶角色作為自定義字段保存到訂單(自定義元數據)user_role,如以下屏幕截圖所示:
查看完整描述

1 回答

?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

以下內容將用戶角色添加為自定義訂單元數據:


// Add the user roles as order meta data

add_action( 'woocommerce_checkout_create_order', 'add_user_roles_to_order_meta_data', 10, 2 );

function add_user_roles_to_order_meta_data( $order, $data ) {

    if( $order->get_user_id() > 0 ) {

        $user = $order->get_user();

        $user_role = reset($user->roles)


        $order->update_meta_data( 'user_role', $user_role );

    }

}

代碼進入您的活動子主題(或活動主題)的function.php文件中。經過測試和工作。


要從WC_OrderObject$order變量獲取用戶角色,您將使用:


$user_role = $order->get_meta('user_role');

或從訂單ID$order_id變量中獲取:


$user_role = get_post_meta($order_id, 'user_role', true);


查看完整回答
反對 回復 2021-05-14
  • 1 回答
  • 0 關注
  • 142 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號