• Resolved kushalforu

    (@kushalforu)


    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

    https://www.ads-software.com/plugins/woocommerce/

Viewing 8 replies - 1 through 8 (of 8 total)
  • It looks like the if should be inside the foreach, otherwise the if tests only the last value for $product_id, not all of them.

    Thread Starter kushalforu

    (@kushalforu)

    Thanks lorro, Your suggestion worked perfectly ! So here i am again pasting the working code for others reference…

    Really thanks for the help.

    So now correct code is for the above said requirement :-

    <?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 == '33' ) {
    	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' );
    ?>

    Hi,

    That’s what I am looking for
    I don’t manage to make it work, can you please help
    1 – You place this function in Theme function.php, right ?
    2 – I had a blank page including <?php ?> so is it the right function :

    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 == ’33’ ) {
    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’ );

    3 – The only value I need to change to adapt to my case is product_id “$product_id == ‘myid”, right ?

    Thanks for help

    It is not a good idea to edit your theme functions.php. This is because any edits will be overwritten the next time the theme is updated.

    You should make a child theme:
    https://codex.www.ads-software.com/Child_Themes

    The above function goes in functions.php for the child theme.

    The first line of functions.php is <?php There is no need for a closing
    ?> tag in this case.

    Yes, just change the id of the product.

    Thanks for your answer and advice

    Hello,
    is there a way to achive the same thing but not for a specific product but for more products like from a certain category or better with a specific keyword?

    I tried to use this code however its not working.

    1 – have used the code exactly how its mentioned

    <?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 == '33' ) {
    	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' );
    ?>

    2 – created a child theme, and created a file functions.php
    3 – added the specific product id identified using the link like
    wp-admin/post.php?post=3796&action=edit where 3796 is the product code. tried a couple of other product codes as well.
    4 – identified the exact user role slug and updated it, tried a couple of other roles as well, just to ensure its not the role In my case it is the user role ‘customer’ that changes to ‘test_role’

    What could have gone wrong?

    Do you have a security plugin or feature that prevents the creation of new users?

    Try deactivating other plugins to make sure none are interfering.

    Add the line:

    var_dump($order_id);

    after the function line to make sure $order_id is something sensible and not null. Repeat after each line with the variable that has changed to try to find out where its not working.

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.