# Selling: Require file attachments Only For Certain Category

**URL:** <https://community.hivepress.io/t/selling-require-file-attachments-only-for-certain-category/1112>\
**Category:** Marketplace\
**Tags:** code\
**Created:** [July 29, 2022, 3:11pm UTC](https://community.hivepress.io/t/selling-require-file-attachments-only-for-certain-category/1112 "2022-07-29T15:11:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rudy](https://community.hivepress.io/letter_avatar_proxy/v4/letter/r/5fc32e/32.png) [@rudy](https://community.hivepress.io/u/rudy)\
**Post date:** [July 29, 2022, 3:11pm UTC](https://community.hivepress.io/t/selling-require-file-attachments-only-for-certain-category/1112/1 "2022-07-29T15:11:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rudy](https://community.hivepress.io/letter_avatar_proxy/v4/letter/r/5fc32e/32.png) [@rudy](https://community.hivepress.io/u/rudy)\
**Post date:** [July 30, 2022, 2:50pm UTC](https://community.hivepress.io/t/selling-require-file-attachments-only-for-certain-category/1112/4 "2022-07-30T14:50:03Z")

</div>

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

```auto
<?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?

---

<div class="post-metadata">

**Author:** ![ihor](https://community.hivepress.io/user_avatar/community.hivepress.io/ihor/32/5_2.png) [@ihor](https://community.hivepress.io/u/ihor)\
**Post date:** [July 30, 2022, 4:50pm UTC](https://community.hivepress.io/t/selling-require-file-attachments-only-for-certain-category/1112/5 "2022-07-30T16:50:04Z")

</div>

Thanks for posting the solution! We also have this snippet in the Gist collection [Make attachments optional for marketplace listings #hivepress #marketplace · GitHub](https://gist.github.com/hivepress/016a42ac96c26e70fa68431b2579e0a0)

---

<div class="post-metadata">

**Author:** ![rudy](https://community.hivepress.io/letter_avatar_proxy/v4/letter/r/5fc32e/32.png) [@rudy](https://community.hivepress.io/u/rudy)\
**Post date:** [July 30, 2022, 5:08pm UTC](https://community.hivepress.io/t/selling-require-file-attachments-only-for-certain-category/1112/6 "2022-07-30T17:08:16Z")

</div>

> [@rudy](#):
>
> Is it possible to chose wich categories have the file attachment?

Hi Ihor,

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

thanks

---

<div class="post-metadata">

**Author:** ![yevhen](https://community.hivepress.io/user_avatar/community.hivepress.io/yevhen/32/23_2.png) [@yevhen](https://community.hivepress.io/u/yevhen)\
**Post date:** [July 31, 2022, 7:57pm UTC](https://community.hivepress.io/t/selling-require-file-attachments-only-for-certain-category/1112/10 "2022-07-31T19:57:09Z")

</div>

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

```auto
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
);

```

---

<div class="post-metadata">

**Author:** ![rudy](https://community.hivepress.io/letter_avatar_proxy/v4/letter/r/5fc32e/32.png) [@rudy](https://community.hivepress.io/u/rudy)\
**Post date:** [July 31, 2022, 10:12pm UTC](https://community.hivepress.io/t/selling-require-file-attachments-only-for-certain-category/1112/11 "2022-07-31T22:12:06Z")

</div>

Thanks, it worked!

---

<div class="post-metadata">

**Author:** ![system](https://community.hivepress.io/user_avatar/community.hivepress.io/system/32/13990_2.png) [@system](https://community.hivepress.io/u/system)\
**Post date:** [August 30, 2022, 10:12pm UTC](https://community.hivepress.io/t/selling-require-file-attachments-only-for-certain-category/1112/12 "2022-08-30T22:12:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shoplocalcraft](https://community.hivepress.io/user_avatar/community.hivepress.io/shoplocalcraft/32/14691_2.png) [@shoplocalcraft](https://community.hivepress.io/u/shoplocalcraft)\
**Post date:** [November 6, 2024, 10:32pm UTC](https://community.hivepress.io/t/selling-require-file-attachments-only-for-certain-category/1112/13 "2024-11-06T22:32:25Z")

</div>

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.

```auto
<?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](https://gist.github.com/hivepress/016a42ac96c26e70fa68431b2579e0a0)

I also tried following these instructions, which did nothing: [Selling: Require file attachments Only For Certain Category](https://community.hivepress.io/t/selling-require-file-attachments-only-for-certain-category/1112)

These instructions also did nothing: [Making digital file attachments optional for specific listing categories](https://community.hivepress.io/t/making-digital-file-attachments-optional-for-specific-listing-categories/4456)

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!

---

<div class="post-metadata">

**Author:** ![andrii](https://community.hivepress.io/user_avatar/community.hivepress.io/andrii/32/2951_2.png) [@andrii](https://community.hivepress.io/u/andrii)\
**Post date:** [November 7, 2024, 1:44pm UTC](https://community.hivepress.io/t/selling-require-file-attachments-only-for-certain-category/1112/15 "2024-11-07T13:44:12Z")

</div>

Hi,

Please check the solution in this topic.
