根据用户角色为 WooCommerce 订单号添加前缀
Adding prefix to WooCommerce order number based on user roles
我正在尝试根据用户角色更改 WooCommerce 订单前缀,目前,此功能完美运行:
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {
$prefix = 'WHLS';
$new_order_id = $prefix . $order_id;
return $new_order_id;
}
现在,我正在尝试通过以下方式根据用户角色更改订单前缀:
Export/Distributor 黄金 - GLD-### (新)
Export/Distributor 银 - SLVR-### (新)
批发 - WHLS-###(当前功能)
我尝试的代码:(此功能代码不加任何订单号前缀)
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {
if ( current_user_can( 'wholeseller' ))
$prefix = 'WHLS';
$new_order_id = $prefix . $order_id;
return $new_order_id;
}
if ( current_user_can( 'distributor_gold' )) {
$prefix = 'GLD';
$new_order_id = $prefix . $order_id;
return $new_order_id;
}
elseif ( current_user_can( 'distributor_silver' )) {
$prefix = 'SLVR';
$new_order_id = $prefix . $order_id;
return $new_order_id;
}
谁能给我指出正确的方向?
使用$order->get_user()
获取与订单关联的用户。
然后 $user->roles
,获取该用户拥有的角色。
所以你得到:
function filter_woocommerce_order_number( $order_number, $order ) {
// Get user
$user = $order->get_user();
// Roles
$roles = (array) $user->roles;
// Compare
if ( in_array ( 'wholeseller', $roles ) ) {
$prefix = 'WHLS';
} elseif ( in_array ( 'distributor_gold', $roles ) ) {
$prefix = 'GLD';
} elseif ( in_array ( 'distributor_silver', $roles ) ) {
$prefix = 'SLVR';
} elseif ( in_array ( 'administrator', $roles ) ) {
$prefix = 'ADMIN';
} else {
$prefix = '';
}
return $prefix . $order_number;
}
add_filter( 'woocommerce_order_number', 'filter_woocommerce_order_number', 10, 2 );
我正在尝试根据用户角色更改 WooCommerce 订单前缀,目前,此功能完美运行:
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {
$prefix = 'WHLS';
$new_order_id = $prefix . $order_id;
return $new_order_id;
}
现在,我正在尝试通过以下方式根据用户角色更改订单前缀: Export/Distributor 黄金 - GLD-### (新) Export/Distributor 银 - SLVR-### (新) 批发 - WHLS-###(当前功能)
我尝试的代码:(此功能代码不加任何订单号前缀)
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {
if ( current_user_can( 'wholeseller' ))
$prefix = 'WHLS';
$new_order_id = $prefix . $order_id;
return $new_order_id;
}
if ( current_user_can( 'distributor_gold' )) {
$prefix = 'GLD';
$new_order_id = $prefix . $order_id;
return $new_order_id;
}
elseif ( current_user_can( 'distributor_silver' )) {
$prefix = 'SLVR';
$new_order_id = $prefix . $order_id;
return $new_order_id;
}
谁能给我指出正确的方向?
使用$order->get_user()
获取与订单关联的用户。
然后 $user->roles
,获取该用户拥有的角色。
所以你得到:
function filter_woocommerce_order_number( $order_number, $order ) {
// Get user
$user = $order->get_user();
// Roles
$roles = (array) $user->roles;
// Compare
if ( in_array ( 'wholeseller', $roles ) ) {
$prefix = 'WHLS';
} elseif ( in_array ( 'distributor_gold', $roles ) ) {
$prefix = 'GLD';
} elseif ( in_array ( 'distributor_silver', $roles ) ) {
$prefix = 'SLVR';
} elseif ( in_array ( 'administrator', $roles ) ) {
$prefix = 'ADMIN';
} else {
$prefix = '';
}
return $prefix . $order_number;
}
add_filter( 'woocommerce_order_number', 'filter_woocommerce_order_number', 10, 2 );