WooCommerce Change User Role on Purchase of Specific Product
-
Hello All,
I am tiring to change user role on Purchase of Specific Product ….Could some one please help out and suggest me whats wrong into below code !
I am putting this action in my theme function.php
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( 'subscriber' ); // Add role $user->add_role( 'contributor' ); } } add_action( 'woocommerce_order_status_completed', 'lgbk_add_member' );
Bit More Detail :-
For Example I have three products 1) Basic 2) Premium & 3)Pro ….So when ever “subscriber” user purchases basic product then his role changes to ‘contributor’ automatically once order will be marked completed !Thanks in Advance
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
- The topic ‘WooCommerce Change User Role on Purchase of Specific Product’ is closed to new replies.