Limit options based on package

Hi Guys,

Me again. Is there a recommended path to take if I want to limit how many images a user can upload to their listing based on which free / paid package they select? Also, I want to add an extra layer between featured and non feature in terms of how the listing shows up in the default list. I want listing to be like this:

First place: Featured
Second place: Verified listings
Last: non verified listings

This will help achieve my goal of basic listing = free with some restrictions (only 2 pictures, only 1 city), verified listing (4 pictures, 2 cities) and featured (10 pictures, 6 cities).

I would be happy to hear your response - currently using meethive theme on two sites.

All the best,

A

Hi @Aizen,

I’m not HivePress staff, but it sounds like this will likely require some advanced customisation, as I don’t think this can all be achieved with a simple snippet or two.

You can use the following PHP snippet to change the maximum number of images that are allowed to be uploaded to a listing.

<?php
add_filter(
	'hivepress/v1/models/listing',
	function( $model ) {
		$model['fields']['images']['max_files'] = 123;

		return $model;
	},
	100
);

However, again, this will likely require further customisations, so you can restrict the amount per Membership type.

I hope this helps!

Edit: I’ve yet to test this, however, I was also interested in the possibility of allowing different limits on the number of images that users can attach to listings, depending upon their Membership. I asked the HivePress AI to assist with this and it came back with the following:

<?php
add_filter(
	'hivepress/v1/models/listing',
	function( $model ) {
		// Get current user's membership packages
		$packages = \HivePress\Models\User_Listing_Package::query()->filter([
			'user' => get_current_user_id()
		])->get()->serialize();

		// Default image limit
		$max_images = 1;

		// Check user's packages and set limits accordingly
		if ( ! empty( $packages ) ) {
			foreach ( $packages as $package ) {
				// Adjust these package names to match your actual membership names
				if ( $package['name'] === 'Premium Package' ) {
					$max_images = 10;
					break;
				} elseif ( $package['name'] === 'Pro Package' ) {
					$max_images = 20;
					break;
				} elseif ( $package['name'] === 'Basic Package' ) {
					$max_images = 5;
					break;
				}
			}
		}

		// Don't apply limits to admins
		if ( ! current_user_can( 'manage_options' ) ) {
			$model['fields']['images']['max_files'] = $max_images;
		}

		return $model;
	},
	100
);
  • Queries the user’s membership packages
  • Sets different image limits based on package names
  • Excludes admins from the limit
  • Uses a default limit for users without packages

To customize:

  • Replace ‘Premium Package’ , ‘Pro Package’ , and ‘Basic Package’ with your actual membership names
  • Adjust the image numbers (10, 20, 5) to your preferred limits

Note: This requires the Memberships extension to work properly.

Cheers,
Chris :victory_hand:

1 Like

Hi,

Please check if the snippet suggested by @ChrisB works for you. If you use Paid Listings, I recommend switching to Memberships, because checking the current membership and applying custom restrictions is much easier since the current user’s memebership is always in the page request context:

$membership=hivepress()->request->get_context('membership');

It may be more complex for the number of cities, if by cities you mean locations added by Geolocation, currently there’s no option to set multiple locations per listing, but if City is a custom attribute, then restricting the number of selected options is possible.

P.S. If you decide to use Memberships, please contact us via support@hivepress.io – if you already purchased the theme, we can provide a discount.

2 Likes