mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-30 11:42:22 +08:00
https://stackoverflow.com/questions/79508971/how-to-update-user-role-after-woocommerce-order-for-a-free-item
21 lines
901 B
Text
21 lines
901 B
Text
add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase', 10, 2 );
|
|
add_action( 'woocommerce_order_status_completed', 'change_role_on_purchase', 10, 2 );
|
|
function change_role_on_purchase( $order_id, $order ) {
|
|
$user = $order->get_user(); // Get the WP_User Object
|
|
|
|
// Loop through order line items
|
|
foreach ( $order->get_items() as $item ) {
|
|
$product_id = $item->get_product_id();
|
|
$variation_id = $item->get_variation_id();
|
|
|
|
if ( $user && in_array('13874904', [$product_id , $variation_id]) ) {
|
|
// Set user as paying customer if not set yet
|
|
wc_paying_customer( $order_id );
|
|
// Check for "subscriber" user role and replace it
|
|
if ( wc_user_has_role( $user, 'subscriber' ) ) {
|
|
$user->set_role( 'special_subscriber' );
|
|
}
|
|
break; // Stop the loop
|
|
}
|
|
}
|
|
}
|