• I’m currently using the User Role Editor plugin along with a function that upgrades a user’s role when they purchase a specific product from the site’s WooCommerce shop. The function to upgrade the user role is:

    add_action( 'woocommerce_order_status_completed', 'upgrade_user_role' );
    
    function upgrade_user_role( $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 == '3156' ) {
    		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( 'magazine-subscriber' );
    	}
    }

    I would like to modify or add to this function so that after so many days the user role that was added (in this case magazine-subscriber) is removed and the only way to get it back is to buy the product again. I would like to avoid having to install any paid plugins if possible, but would consider it if necessary.

    If anyone has any advice on how to achieve this I would really appreciate it!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    If you have modest coding skills you can create your own plugin to handle this (or add the code to an existing custom plugin or child theme, perhaps where the above code is?).

    Before getting into the plugin, alter your existing code to store a timestamp when the user is promoted. Don’t store a date string, a timestamp makes the time difference math soooo much easier. To account for return customers who were demoted, then bought something else, alter the new user logic to only happen if not is_user_logged_in(), otherwise get the current user object.

    Now the additional demotion code. You really only need to schedule an event to periodically run. What the callback function would do is query for all users whose promotion timestamp is older than the prescribed time and demote all such users. This could all be done as a single query, though I’m not sure I could write such a query, maybe you could.

    Instead I’d loop through each user returned as having old timestamps and demote them using PHP. It’d be a good idea to replace the timestamp with null so the user is eliminated from further queries.

    Thread Starter alexrdc

    (@alexrdc)

    Thanks for the advice, I would think that approach would work for this. I’m having a hard time figuring out the best way to add the timestamp when the function is executed though.

    Also, I need to be able to do this for several products, each assigning their own user role which will be last for 30 days. I’m assuming that once I get this set up properly that won’t be an issue using this method.

    Moderator bcworkz

    (@bcworkz)

    I would think just adding this to the user role upgrade part would do it:
    update_user_meta( $user->ID, 'magazine-subscriber', time());
    Or is there some twist I’m not aware of?

    Then when the scheduled demotion event runs you query for all users where the ‘magazine-subscriber’ user meta is less than time() - (30*24*60*60) and demote everyone returned. Also remove the user meta or set it to null.

    Multiple products/roles would be no problem since the timestamp is unique to each role/meta key added.

    Did you ever finish the code for your WooCommerce store? We are doing the same for a free trial for subscriptions product… Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Automatically Change User Role After x Days’ is closed to new replies.