Today i will show you how to add user to group after product purchase. The snippet presented, applies if you use Groups plugin from itthinx and WooCommerce plugin.
However, you can use the hook to apply whatever you want. There are several occasions where you want to do something, when an order is complete.
The use case is this. You want to add the new user/customer to a specific group, after buying a specific product. The steps below explain the logic of the snippet following.
First we disable guest checkout in WooCommerce.
We want to create a new user to the site, so we force customers to register. This option is under WooCommerce Settings, in Checkout tab.
Second, everything must happen after the purchase.
So, we need a hook to trigger after the purchase is made. In other words, after the order is complete. The suitable hook for the process is this. Another useful detail is that it triggers whether you use auto-complete or change the order status manually.
woocommerce_order_status_completed
Third, get the order data.
We need the order data to check if the desired product is involved. Also, we need to extract the user id from the order, in order to add him to the desired group.
Fourth, add user to group.
The process is simple. We get the user id and we add user to group with the create method of Groups_User_Group class like this. The API of Groups is availiable in the documentation page.
Groups_User_Group::create( array( 'user_id' => $my_user_id, 'group_id' => $group_id ) );
Finally, the code snippet.
Put this snippet in your child theme’s function.php file and let it do the job.
add_action( 'woocommerce_order_status_completed', 'gt_add_user_to_group' ); function gt_add_user_to_group( $order_id ) { $order = new WC_Order( $order_id ); $my_user_id = $order->user_id; $items = $order->get_items(); foreach ( $items as $item ) { if ( $item->get_product_id() == 2399 ) { if ( $group = Groups_Group::read_by_name( 'Premium' ) ) { // Add user to group 'Premium' Groups_User_Group::create( array( 'user_id' => $my_user_id, 'group_id'=>$group->group_id ) ); } } } }
Try a test purchase and visit the Users page to see results.
What other applications can you think of when an order is complete? Share your ideas in the comments.
Amanda says
Aaaaand I just saw the line about order status being completed. That is probably my problem. I’ll check with one of my test orders to confirm.
I want to trigger this event when checkout completes as the order itself contains items that will be shipped/picked up at a later date – but I want to give access to paid sections of the site right away. I guess I need to find the do_action event for checkout complete instead of order complete and hook into that instead.
Amanda says
Yep – it worked when I completed the order!! Now to find the right place to hook it into! #Progress Thank you! I learned a LOT from this example!
George says
Good to know that you resolved it!!! 🙂
Perhaps you should have a look at these hooks:
woocommerce_checkout_order_processed
woocommerce_new_order
I haven’t tried them though coz IMO it’s better to grant privileges when the order is completed ie when the payment is complete.
Cheers,
George
George says
Hi Amanda,
To be honest it’s been a while since i last used it, so everything is possible.
Please keep in mind that the order status must be completed.
What versions of WooCommerce and Groups do you use?
I will have a look using the latest versions for both and will keep you posted.
Thanks.
Amanda says
Thanks George! Any help you can offer is greatly appreciated. It’s entirely possible that the issue is entirely user error. I consider myself reasonably adept at HTML and CSS – but PHP is not my strong suit (it’s been about 2 years since I’ve done anything with PHP). I got thrown into the deep end with this WordPress site, so I’m learning how everything works as I go – and in no particular logical order. 🙂
I’m using Groups Version 2.0.2 and Woocommerce version Version 2.6.14
Thanks in advance!
Amanda says
WordPress newbie here, and I might be missing something really basic – but the snippet isn’t working for me. I’ve modified the code snippet to reflect the product I’m interested in and the name of the group I want the user added to.
I’ve put this snippet in my functions.php file – but nothing is happening when I place an order.
Is this snippet still good for the current version of Woocommerce and groups?