Selling: Require file attachments Only For Certain Category

Hello, in hivepress settings for the selling, I checked the

“Require file attachments” because I need vendors to be able to sell ebooks on my category Ebooks.

But for other categories makes no sense in my marketplace to have a file attachment as required.

Could this item be optional or can I only restrict it to the category ebooks?

Thanks

If it can help someone, to make file attachment optional, this is the snippet

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

Is it possible to chose wich categories have the file attachment?

Thanks for posting the solution! We also have this snippet in the Gist collection Make attachments optional for marketplace listings #hivepress #marketplace · GitHub

Hi Ihor,

Is it possible to chose wich categories have the file attachment?

thanks

Please try this PHP snippet but please note that it needs additional testing. Please change 1,2,3 on the categories id where you want to show this field

add_filter( 
	'hivepress/v1/models/listing/attributes', 
	function ($attributes){
		if(isset($attributes['attachments'])){
			$attributes['attachments'] = array_merge($attributes['attachments'], ['categories' => [1,2,3]]);	
		}
		
		return $attributes;
	},
	1000
);

Thanks, it worked!

In the Listings Settings, there is currently an option to “Require file attachments”. I would like my Marketplace to have listing options for both digital goods and physical goods. I hope to achieve this by requiring File Attachment for a specific category, and allowing file attachment to be an option for other categories.

I am using the Code Snippet plugin to add PHP customizations, but it doesn’t seem to work completely. I would like help knowing why this doesn’t work.

<?php
add_filter(
	'hivepress/v1/models/listing/attributes',
	function( $attributes ) {
		if ( isset( $attributes['attachments'] ) ) {
			$attributes['attachments']['edit_field']['required'] = false;
			// removes requirement for uploading a file in the listing. uploading file is now optional.
		}
		if ($attributes['categories'] == ['digital-downloads']) {
			$attributes['attachments']['edit_field']['required'] = true;
			// requires uploading a file in the listing if the category is digital-downloads
		  }
 
		return $attributes;
	},
	1000
);

Basically, what I am trying to do here is to first make the file upload optional, and then make the file upload required if the selected category is Digital Downloads. The current result is that ALL categories allow OPTIONAL file upload. The Digital Downloads category is not requiring the file upload, the second argument doesn’t seem to be executed. I part of the code from here: Make attachments optional for marketplace listings #hivepress #marketplace · GitHub

I also tried following these instructions, which did nothing: Selling: Require file attachments Only For Certain Category

These instructions also did nothing: Making digital file attachments optional for specific listing categories

Please advise, I appreciate your assistance!

EDIT: I can see your reply, andrii, but there is no way for me to respond. As I put in my post already, I tried this solution and it does nothing at all - there is no change to the behavior of the listing form. I would very much appreciate knowing why nothing I am trying works, as I just paid for this software!

Hi,

Please check the solution in this topic.