3

I am trying to use this snippet of code to update my users from the default role of 'Subscriber' to the role of 'Premium' on the purchase of a product from my store.

add_action( 'woocommerce_order_status_completed','change_role_on_purchase' );
function change_role_on_purchase( $order_id ) {
$order = wc_get_order( $order_id );
$items = $order->get_items();

$products_to_check = array( '416' );

foreach ( $items as $item ) {
    if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) {
        $user = new WP_User( $order->user_id );

        // Change role
        $user->remove_role( 'Subscriber' );
        $user->add_role( 'Premium' );

        // Exit the loop
        break;
    }
}
}

I only have 1 product in my store and it has the product ID 416 (which I have inserted in the code).

I have put this into functions.php, but i'm not having any luck. The role isn't being updated after any successful purchase. Any ideas?

googleyberry
  • 73
  • 3
  • 10

4 Answers4

2

Have a try with this one:

function change_role_on_purchase( $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 == '416' ) {
            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( 'premium' );
        }
    }
}

add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase' );
jjj
  • 965
  • 8
  • 23
2

In case someone is interested in this. If your customer wants to have in the future more products that will update the buyers role after purchase, instead of manually adding the product ids in the function, you can use this one

$products_to_check = wc_get_products( array( 'return' => 'ids', 'tag' => array('your tag here') ) );

Your customer can edit by him/herself a product and assign the product tag, that will allow the update of the user role.

1

what if we want to check product category instead of product id? How do we tweak this? The code below is for verifying multiple products with their respective IDs.

 add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase' );
function change_role_on_purchase( $order_id ) {
    $order = wc_get_order( $order_id );
    $items = $order->get_items();

    $products_to_check = array( '27167', '27166' );

    foreach ( $items as $item ) {
        if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) {
            $user = new WP_User( $order->user_id );

            // Change role
            $user->remove_role( 'friends' );
            $user->add_role( 'customer' );

            // Exit the loop
            break;
        }
    }
}
  • 1
    There is already a similar answer, which is accepted. So why you have posted this one? Any reasons? – Sinto Mar 08 '19 at 04:32
  • Sinto, I am fairly new onto wordpress and stackoverflow. Althought my answer is being hinted for. But I am unable to put the actual code together. There is no where a full code is given as to what can be done if we need to check purchase from a particular category. Can help? – Debashish Pasha Bairagi Mar 12 '19 at 02:46
0

Same thing here... Please keep in mind that woocommerce_order_status_processing means just that: that the order status is set to "processing". This does NOT mean that the order is "complete", meaning that it has been paid for. If you use this hook, you run the risk of making content available to your customer even though his order may not have been paid for. This is what woocommerce_order_status_completed is there for, but that one doesn't seem to work for virtual products, which get the order status automatically set to "completed".

woocommerce_order_status_completed only worked for me with virtual products disabled/unchecked in the products section, meaning I had to manually "complete" the order in order to trigger this hook. Still looking for a solution...