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

Sorry to revive an old post, but I found out my SQL query was not correct. Well, it was correct (or good enough) when I had only two packages (including the free one).

But now I have 3 packages (including the “free” one).
The paying packages are “premium” and “pro”. Pro is better than premium, meaning it gives user access to extra features not available in the premium package.

But the sql query will show in the body tag classes premium-pack pro-pack, even if the user only subscribed to premium-pack. Bummer !

We have this page in the user dashboard (/account/listing-packages/), showing the currently subscribed package for the user :

I think I should find the underhand SQL query that allows to retrieve this info.
I went as far as setting up the debug bar, but I am still lost.

Could you put me on the way ? I need the SQL query that will show me which package the user is currently on.

Cheers !

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

Hi,

Thank you for your solution, it will be useful for our community.

Hello again,

The solution I provided here above is just a workaround.
But I saw here and there some more appropriate (hivepress’) ways to implement this way of knowing which package the current user is currently having, using User_Listing_Package::query().

My point is I would like to conditionally enable special extra features like multi-category select, depending on current user package, but I am stuck, right now.

Could you please help me with that ?

Here the best I could come up with, combining my old code with multi categories feature :

add_filter(
	'hivepress/v1/forms/listing_submit',
	function( $form ) {
		$current_pack = get_customer_orders_with_products(); 
		//$form['description'] = $current_pack; //for debugging 
		if(isset($form['fields']['categories'])){
			if ($current_pack =='premium-pack' || $current_pack =='pro-pack'){
				$form['fields']['categories']['multiple'] = true;
				unset( $form['fields']['categories']['attributes']['data-multistep'] );				
			} 
		}
		return $form;
	},
	1000
);

I would like therefore to edit get_customer_orders_with_products(), with
User_Listing_Package::query().

Also why it’s not possible for a user to edit a listing category afterwards ?
It does not make sense to me that you can edit pretty much everything in the listing BUT the category.

And since user select package AFTER creating the listing, the feature I would like to offer to premium users can’t be implemented (at least for the first listing).

@ihor, any idea :folded_hands: ?

Hi,

You can fetch user packages and check them this way:

$packages = \HivePress\Models\User_Listing_Package::query()->filter(
	[
		'user' => 123,
	]
)->get();

foreach ( $packages as $package ) {
	// check package details, e.g. name using $package->get_name()
}

You can find more examples here Making queries | Developer Docs The available fields for filtering depend on the model.

Currently, category is the only field that can’t be changed later because it’s like a listing type, e.g. classifieds don’t allow this either because categories can have different required listing fields (e.g. it’s not possible to switch a real estate listing into a car listing after publishing because this would require re-filling the form). We plan to make behavior this optional, though.

I understand your explanation. It now makes sense.

By the way, I did not know how you could tie specific attributes to a category.
This post helped me understand it all.

However, with the hack enabling multiple categories, it does not work so well (unless you remove all categories).
But granted, that’s an edge case. :wink:

Thank you for your answer.

1 Like