Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将用户角色作为自定义元数据添加到电子商务订单_Php_Woocommerce_Custom Fields_Orders_User Roles - Fatal编程技术网

Php 将用户角色作为自定义元数据添加到电子商务订单

Php 将用户角色作为自定义元数据添加到电子商务订单,php,woocommerce,custom-fields,orders,user-roles,Php,Woocommerce,Custom Fields,Orders,User Roles,在WooCommerce中,我想知道是否可以使用user\u rolemeta键将用户角色作为自定义字段保存到订单(自定义元数据),如以下屏幕截图所示: 欢迎提供任何帮助。以下内容将添加用户角色作为自定义订单元数据: // Add the user roles as order meta data add_action( 'woocommerce_checkout_create_order', 'add_user_roles_to_order_meta_data', 10, 2 ); func

在WooCommerce中,我想知道是否可以使用
user\u role
meta键将用户角色作为自定义字段保存到订单(自定义元数据),如以下屏幕截图所示:


欢迎提供任何帮助。

以下内容将添加用户角色作为自定义订单元数据:

// 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\u Order
对象
$Order
变量中获取用户角色,您将使用:

$user_role = $order->get_meta('user_role');
或从订单Id
$order\u Id
变量:

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