我试图改变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;
}
现在,我正试图通过以下方式基于用户角色更改订单前缀:导出/分发Gold-GLD-#####(新)导出/分发Silver-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-
然后,$user-
所以你得到:
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 );