Skip to content

Instantly share code, notes, and snippets.

@troydean
Created March 3, 2014 10:51
Show Gist options
  • Select an option

  • Save troydean/9322593 to your computer and use it in GitHub Desktop.

Select an option

Save troydean/9322593 to your computer and use it in GitHub Desktop.
WooCommerce Change User Role on Purchase of Specific Product
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 == '48' ) {
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( 'subscriber' );
}
}
add_action( 'woocommerce_order_status_completed', 'lgbk_add_member' );
@swapri

swapri commented Mar 9, 2017

Copy link
Copy Markdown

what if i have more than one product ?

@eddywhere

eddywhere commented Sep 12, 2017

Copy link
Copy Markdown

Thank you so much!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment