Allow sellers to limit quantity on specific category

there is some code snippet to allow the sellers from a single category to limit quantity?

Yes, please try using Paid Listings extension for this, you can add category-specific packages (e.g. a free package with a limit for specific categories).

Hi yevhen, thanks for your response.

But I dont want to limit the quantity of listings that a costumer can post but the quantity of sales like a stock of the product that he’s listing.

I want to the costumer when he’s creating a new listing has a option to put the stock of his product. Imagine a event directory site that the costumer is creating a conference that has only 1000 tickets.

If you use the Marketplace extension please try using it’s Allow sellers to limit quantity setting in HivePress/Settings/Listings/Selling section. Then the quantity will be decreased on every sale, and the listing will be hidden automatically when the last item is sold.

1 Like

there is a snippet code to limit quantity option in only one category?

Please let me know if you mean just hiding the Quantity field for buyers or also removing the ability for sellers to limit quantity in the listing edit form. It may be possible with a simple code snippet, but there may be unpredictable issues on checkout - this requires testing.

both, remove hiding quantity field for buyers and removing the ability for sellers to limit quantity in the listing form

Please try this PHP snippet and please change 1,2,3 on the categories ids where you want to show these fields

add_filter(
	'hivepress/v1/models/listing/attributes',
	function( $attributes ) {
		if ( isset( $attributes['quantity'] ) ) {
			$categories = [ 1, 2, 3 ];

			$attributes['quantity']['categories']  = $categories;
		}

		return $attributes;
	},
	1000
);

add_filter(
	'hivepress/v1/forms/listing_buy',
	function( $form_args, $form ) {

		$categories_id = [ 1, 2, 3 ];

		$listing = $form->get_model();
		
		if ( isset( $form_args['fields']['_quantity'] ) && array_diff((array)$listing->get_categories__id(), $categories_id) ) {
			unset($form_args['fields']['_quantity']);
		}

		return $form_args;
	},
	1000,
	2
);

hi yevhen this works great, but how to make quantity attribute required when creating a listing?

Please try this PHP snippet and please change 1,2,3 on the categories ids where you want to show these fields

add_filter(
	'hivepress/v1/models/listing/attributes',
	function( $attributes ) {
		if ( isset( $attributes['quantity'] ) ) {
			$categories = [ 1, 2, 3 ];

			$attributes['quantity']['categories']  = $categories;
			$attributes['quantity']['edit_field']['required']  = true;
		}

		return $attributes;
	},
	1000
);

add_filter(
	'hivepress/v1/forms/listing_buy',
	function( $form_args, $form ) {

		$categories_id = [ 1, 2, 3 ];

		$listing = $form->get_model();
		
		if ( isset( $form_args['fields']['_quantity'] ) && array_diff((array)$listing->get_categories__id(), $categories_id) ) {
			unset($form_args['fields']['_quantity']);
		}

		return $form_args;
	},
	1000,
	2
);

hi yevhen, in the selected category there’s no option to buyer select the quantity its only possible to buy one by one. how to fix it? make the buyer able to select quantity?

Please make sure that the Allow buyers to select quantity setting is enabled in HivePress/Settings/Listings

Hi yevhen I also needed this function. Thanks for providing it, but how do I find out the “id” number of each category? Or where can I see it ?
Thanks

Please open Listings/Categories and please try to edit one of the categories. You’ll see the category ID in the browser’s address bar where the category ID is the number between category&tag_ID= and &post_type

Thanks yevhen but it doesn’t work for me. I have purchased the expert hive theme and the booking and marketplace plugins, and I have configured my website so that the seller sets a minimum and maximum booking amount in the HIVEPRESS/SETTING/ Allow multiple places per booking option.
In the code above you explain how to remove the quantity for seller and buyer but I need my “Allow multiple places per booking” fields to be able to choose in which categories they are shown for both buyer and seller.
Thank you very much.

Translated with DeepL Translate: The world's most accurate translator (free version)

Please try this PHP snippet and please change 1 on the categories ids where you want to show these fields

add_filter(
	'hivepress/v1/models/listing/attributes',
	function( $attributes ) {
		$categories = [ 1 ];
		
		if ( isset( $attributes['booking_min_quantity'] ) ) {
			$attributes['booking_min_quantity']['categories']  = $categories;
		}
		
		if ( isset( $attributes['booking_max_quantity'] ) ) {
			$attributes['booking_max_quantity']['categories']  = $categories;
		}

		return $attributes;
	},
	1000
);

add_filter(
	'hivepress/v1/forms/booking_make',
	function( $form_args, $form ) {

		$categories_id = [ 1 ];

		$listing = $form->get_model()->get_listing();

		if ( isset( $form_args['fields']['_quantity'] ) && array_diff((array)$listing->get_categories__id(), $categories_id) ) {
			unset($form_args['fields']['_quantity']);
		}

		return $form_args;
	},
	1000,
	2
);
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.