Make quantity field optional

The following snippet does not work when applied to the ‘quantity’ attribute/field:

add_filter(
	'hivepress/v1/models/listing/attributes',
	function( $attributes ) 
	{
		if ( isset( $attributes['quantity'] ) ) 
		{
			$attributes['quantity']['edit_field']['required'] = false;
		}
		return $attributes;
	},
);

Likewise, the following snippet does not work when applied to the ‘extras’ attribute/field:

add_filter(
	'hivepress/v1/forms/listing_update',
	function( $form ) {
		$form['fields']['extras']['label'] = 'shipping options';

		return $form;
	},
	1000
);

Both snippets work when applied to other listing attributes/fields.

Hi,

Please make sure you add these snippets using this doc: How to add custom code snippets - HivePress Help Center. Also, please disable third-party plugins and customizations (if there are any) and check if this issue persists. If you use a caching plugin, make sure that caching is disabled for logged-in users.

​I hope this is helpful to you.

Thanks for the reply, however this does not help.

As I said, the snippets work fine in general, but for specific fields (which I mentioned) they do not. If there was a caching-related or plugin-related problem I would expect it to impact all fields and now just one or two.

For example, the first snippet works perfectly find when applied to the ‘price’ field, but not for the ‘quantity’ field.

P.S. Why is it that my license code isn’t giving my user avatar the star?

Sorry for the confusion. This issue may occur if these fields are not attributes, and they are added as fields on the form level. In this case you have to filter the form instead of attributes so there’s a different hook, for listings it’s hivepress/v1/models/listing, then you can change $listing['fields'] array inside it. You can also check the hivepress/v1/forms/listing_update hook.

Hope this helps

Hi @ihor , could you please supply me with the full snippet to change the ‘quantity’ field on the Listings page to optional?

P.S. Why is it that my license code isn’t giving my user avatar the star?

This works for the ‘price’ field, but not for the ‘quantity’ field.

add_filter(
    'hivepress/v1/forms/listing_update',
    function( $form ) {
        $form['fields']['quantity']['required'] = false;
        $form['fields']['price']['required'] = false;

        return $form;        
    },
    1000
);

Please provide a snippet that works for the ‘quantity’ field.

Please try the solution described here Quantity and purchase note required We tested it locally and it seems to work. If it doesn’t work make sure that it’s added correctly, you can also disable other HivePress extensions that may conflict with the quantity field just to check, e.g. Bookings if you have it installed. We tested this locally with HivePress and Marketplace only.

Thanks for reporting the support badge issue, I guess there’s something with the payment provider API, I’ll test this further.

@ihor this is the first snippet I tried. It doesn’t work. The ‘quantity’ field remains mandatory.

make sure that it’s added correctly

It’s added like all my other snippets which work. (Plugin: Code Snippets)

disable other HivePress extensions that may conflict with the quantity field

I also tried with only HivePress and Marketplace activated, and it’s still not working.

Remember, I am trying to make the quantity field optional, not mandatory. The snippet you refer to is for making that field mandatory. Have you tested making the field optional?

In the meantime I created a new Listing attribute to replace the built-in quantity field, and did not make it mandatory.

So moving on the the second snippet that didn’t work …

add_filter(
	'hivepress/v1/forms/listing_update',
	function( $form ) {
		$form['fields']['extras']['label'] = 'shipping options';

		return $form;
	},
	1000
);

Why doesn’t this work?

I have also tried this snippet without success:

add_filter(
	'hivepress/v1/forms/listing_update',
	function( $form ) {
		if ( isset( $form['fields']['price_extras'] ) ) {
			$form['fields']['price_extras']['label'] = 'shipping options';
		}

		return $form;
	},
);

Hi,

  1. Regarding the quantity field, you specified that you need to make it a required field, and now you have indicated that you need to make it optional. Note that this field is optional by default, so you don’t need any additional PHP snippets.

  2. Please use this PHP snippet:

add_filter(
	'hivepress/v1/forms/listing_update',
	function( $form ) {
		if ( isset( $form['fields']['price_extras'] ) ) {
			$form['fields']['price_extras']['label'] = 'shipping options';
		}

		return $form;
	},
1000
);

In the previous one, you did not specify “1000”.

Hi @andrii ,

  1. No I didn’t. You can see in my opening post that the snippet has ‘required’ set to ‘false’. If you are referring to the name of the thread - I created the thread with a different title than the one it has now - someone else changed it!

Furthermore, if I enable the ‘Allow sellers to limit quantity’ option (HivePress → Settings → Listings), the resulting ‘Quantity’ field is not optional:

Screenshot from 2024-08-19 15-41-56

  1. That worked! Thanks!
    Is it possible to limit the selection to only one extra?

@andrii it looks like you are the one who edited the thread title … or!? :grin:

Hi,

I see. We are changing the titles to make it easier for users to find answers on the forum. Since the quantity field is optional by default, we changed the title, you can check the screenshots for more details:
https://www.awesomescreenshot.com/image/50129685?key=6caa60c91afd08116949c7fc90fc70cd

https://www.awesomescreenshot.com/image/50129699?key=c977517a0e1fead0a1adc372aed8c878

There is no such feature as limit selection, you would need a custom implementation on the hivepress/v1/forms/listing_update/errors hook and check how many extras are trying to be saved and generate an error based on that.

Also, please note that while we try to provide coding-related guidance to help third-party developers and to prevent getting stuck when it comes to customizing our products, unfortunately, we can’t provide specific guidance along with ready-made code snippets; customization is beyond the support scope included with the purchase Support Policy | HivePress

@andrii

  1. that is so weird.
    I also have this option enable …
    Screenshot from 2024-08-20 15-51-07
    … but for me the ‘Quantity’ field is not optional.
    Screenshot from 2024-08-20 15-51-39
    How is that possible?

  2. Sorry if I wasn’t clear enough. Currently if the listing includes 3 extras, they appear on the listing as checkboxes, and the customer can select more than one extra.
    I’m looking for a way to make it so that the customer can only select one extra. Maybe changing from checkboxes to radio buttons would do the trick?

Hi,

  1. I see. Please disable third-party plugins and customizations (if there are any) and check if this issue persists. If you use a caching plugin, make sure that caching is disabled for logged-in users. Please note that you need to disable all plugins except for HivePress extensions.

  2. Yes, perhaps a radio button will help with this, but it will require a custom implementation using the hivepress/v1/forms/listing_buy hook.

Sorry for the delay in replying.

I tested with all plugins disabled other than the HivePress plugins/extensions and WooCommerce, and the ‘Quantity’ field shows up as mandatory:

image

Hi,

Please send temporary WP access to support@hivepress.io with details for reproducing this issue, and we’ll check it (please send only the link, without login and password). You can create a temporary access link using this plugin: Temporary Login Without Password – WordPress plugin | WordPress.org.

Done (Email sent)! :white_check_mark:

Hi,

We have checked this issue and it is caused by customizations. You had the ListingHive child theme activated, where these customizations were, and when the parent theme is enabled, everything works correctly.