How do I know which package a user is currently on?

For lack of a better solution, I return the last woocommerce order for that user.
And rummage in the item (only one item/product per order in HivePress) for the given order to see if it has ‘premium-pack’ or ‘pro-pack’.

Updated code (for those who would be interested) :

function get_customer_orders_with_products() {
  
  $current_user = wp_get_current_user();
	$args = [
        'customer_id' => $current_user->ID,
        'limit'       => 1, // just returns last order 
        'orderby'     => 'date',
        'order'       => 'DESC',
        'status'      => ['wc-completed', 'wc-processing', 'wc-on-hold'], 
    ];

    $orders = wc_get_orders($args);
    
    foreach ($orders as $order) {
        foreach ($order->get_items() as $item) {
			$product = $item->get_product();
			if($product){//only one product per order anyway, so no need to loop endlessly
				return $product->get_slug(); //pro-pack or premium-pack (any other values not taken into account by CSS hack anyway)			
			}
        }
    }
}


/* ##############
* Add custom body class for package
*/
function add_custom_body_class_for_package($classes) {

	$result = get_customer_orders_with_products(); 
	if (!empty($result)) {
			$classes[] = $result; //add package name to body classes 
	}else{
			$classes[] ='no-package';		
	}    
    return $classes;
}
add_filter('body_class', 'add_custom_body_class_for_package');

Still, I don’t know if the plan is still current/valid, any suggestion welcome.

Not perfect, but good enough.

Cheers !

1 Like