I would like to make a members only section for WooCommerce customers who have made completed purchases, by assigning them a new role as the default for all registered users are "customer".
I've stumbled across some code which can solve this, however with over 200 products in store, it would be a hassle to list all the products individually.
Is there anyway to give a customer who makes ANY purchase a new role?
This is the original code:
add_action( 'woocommerce_order_status_completed', 'wpglorify_change_role_on_purchase' );
function wpglorify_change_role_on_purchase( $order_id ) {
// get order object and items
$order = new WC_Order( $order_id );
$items = $order->get_items();
$product_id = 56; // that's a specific 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( 'editor' );
}
}
}