Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter rdcalex

    (@rdcalex)

    Thanks for the replies.

    It seems that only one change occurs at all when multiple products are purchased that should change the user role. This means that whether I have it store user meta data or add capabilities, only one of the products will affect the user’s status. I’ve tried combining the functions into one with multiple if statements and still only one product has any effect.

    Edit: There was a simple error with the code that I’ve solved. The if statements needed to be within the foreach statement. It’s working fine now.

    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'];
    
    		// Fall 2014
    
    		if ( $order->user_id > 0 && $product_id == '3326' ) {
    			update_user_meta( $order->user_id );
    
    			update_user_meta( $order->user_id, 'fall_2014_expiration', time() + (30 * 24 * 60 * 60) );
    
    			$user = new WP_User( $order->user_id );
    
    			// Add role
    			$user->add_role( 'fall-2014-subscriber' );
    		}
    
    		// Summer 2014
    
    		if ( $order->user_id > 0 && $product_id == '3800' ) {
    			update_user_meta( $order->user_id );
    
    			update_user_meta( $order->user_id, 'summer_2014_expiration', time() + (30 * 24 * 60 * 60) );
    
    			$user = new WP_User( $order->user_id );
    
    			// Add role
    			$user->add_role( 'summer-2014-subscriber' );
    		}
    	}
    }
    Thread Starter rdcalex

    (@rdcalex)

    The user role plugin I’m using (User Role Editor) already allows for users to have as many additional roles assigned as necessary. If a user purchases each of those items in the code sample I posted separately they will have the role of Subscriber and Other Roles of fall-2014-subscriber and summer-2014-subscriber. However, when those same items are purchased simultaneously only the first is applied.

Viewing 2 replies - 1 through 2 (of 2 total)