Changing role on purchase
-
Hello, I have a code to add role on purchase. How do I add multiple roles with multiple purchases ? For example I have 3 products with Id’s = “1”, “2”, “3”
and if you purchase “1” and “3” you get certain role. Also with “1”, “2” e.t.c
add_action( ‘woocommerce_order_status_completed’, ‘tb_change_role_on_purchase’ );
function tb_change_role_on_purchase( $order_id ) {
// get order object and items
$order = new WC_Order( $order_id );
$items = $order->get_items();$product_id = 26; // 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( 'um_custom_role_2' ); } $order = new WC_Order( $order_id ); $items = $order->get_items(); $product_id = 24; // Product ID 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( 'um_custom_role_3' ); }The blog I need help with is: (visible only to logged in users)
-
Hi there,
It sounds like you need help with the WooCommerce plugin but have landed on the support forum for WordPress.com sites instead.
We don’t offer WooCommerce on WordPress.com (unless you are upgraded to our WordPress.com Business or eCommerce Plan) so we are not your best point of contact, but the good news is that help is available by contacting WooCommerce staff here:
https://wordpress.org/support/plugin/woocommerce/#new-topic-0
Thanks!
- The topic ‘Changing role on purchase’ is closed to new replies.