vapvarun icon

Change Role with WooCommerce at Order Mark Complete

vapvarun | PRO | 02/28/23 05:45:02 AM UTC | 0 ⭐ | 1165 👁️ | Never ⏰ | []
PHP |

600 B

|

None

|

0 👍

/

0 👎

add_action( 'woocommerce_order_status_completed', 'vap_change_role_on_purchase_gold' );
 
function vap_change_role_on_purchase_gold( $order_id ) {
 
// get order object and items
    $order = new WC_Order( $order_id );
    $items = $order->get_items();
 
    $product_id = 2355; // that's a specific product ID
 
    foreach ( $items as $item ) {
 
        if( $product_id == $item['product_id'] && $order->user_id ) {
            $user = new WP_User( $order->user_id );
 
            // Remove old role
            $user->remove_role( 'customer' ); 
 
            // Add new role
            $user->add_role( 'pmpro_role_3' );
        }
    }
}

Comments