我有六个不同的用户角色-A,B,C,D,E和F。我想在woocommerce购买中为A,B,C分配特定的角色。
Role A -> Role D
Role B -> Role E
Role C -> Role F
更新
添加了我正在尝试的代码,但是并没有更改角色。
add_action( 'woocommerce_order_status_completed', 'custom_role_on_purchase', 100, 1 );
function custom_role_on_purchase( $order_id ) {
$order = wc_get_order( $order_id );
$user = $order->user_id;
$user_meta = get_userdata( $order->user_id );
$user_roles = $user_meta->roles;
if ( in_array( 'Role A', $user_roles ) ) {
$user->set_role( 'Role D' );
} else if ( 'Role B', $user_roles ) ) {
$user->set_role( 'Role E' );
} else if ( 'Role C', $user_roles ) ) {
$user->set_role( 'Role F' );
}
}
答案 0 :(得分:0)
尝试此代码。
add_action( 'woocommerce_order_status_completed', 'custom_role_on_purchase', 100, 1 );
function custom_role_on_purchase( $order_id ) {
$order = wc_get_order( $order_id );
$user = $order->user_id;
$user_meta = get_userdata( $order->user_id );
$user_roles = $user_meta->roles;
$Masteruser = new WP_User( $user_meta->ID );
if ( in_array( 'Role A', $user_roles ) ) {
$Masteruser->remove_role( 'Role A' );
$Masteruser->set_role( 'Role D' );
} else if ( in_array('Role B', $user_roles ) ) {
$Masteruser->remove_role( 'Role E' );
$Masteruser->set_role( 'Role E' );
} else if ( in_array('Role C', $user_roles ) ) {
$Masteruser->remove_role( 'Role F' );
$Masteruser->set_role( 'Role F' );
}
}