Skip to content

Instantly share code, notes, and snippets.

@waqasnu
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waqasnu/950eafc3e9b4855afc88 to your computer and use it in GitHub Desktop.
Save waqasnu/950eafc3e9b4855afc88 to your computer and use it in GitHub Desktop.
Change role based on product purchase
function lgbk_add_member( $order_id ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
if ( $order->user_id > 0 && $product_id == '2138' ) {
update_user_meta( $order->user_id, 'paying_customer', 1 );
$user = new WP_User( $order->user_id );
// Remove role
$user->remove_role( 'customer' );
// Add role
$user->add_role( 'toolkitmembers' );
}
}
}
add_action( 'woocommerce_order_status_completed', 'lgbk_add_member' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment