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 :
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.
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.
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).
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.