我有六个不同的用户角色 - 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' );
}
}
试试这段代码。
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' );
}
}