Optional Non Mandatory Location fields for specific category

I want to make location field optional for specific category

Please let me know as i added license key

Unfortunately there’s no option for this, this would require code customizations. Please check our code snippet collection for sample snippets Search · user:hivepress · GitHub

Can you let me know more about code customizations because i tried its not working
also checked snippet but can’t found solution

Thank you for waiting. Please try this PHP code snippet. Please change 1,2,3 in the code snippet on listing categories IDs where you want to set the location field required.

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

I tested above code but this code is to show location field for specific category
like if i add 1,2,3 it will show location field for that category only, for other categories it will hide location field

but i want to just change location field to not required(Non mandatory) for specific category
Can you please check?

Unfortunately, there’s no simple code snippet for this, it would require a custom implementation. If customizations beyond the available features are required for your site, please consider hiring someone for custom work Fiverr - Freelance Services Marketplace

Hi doli.gohel,

Here is my code snippet to make location field not required for a specific category (it’s possible it’s not the cleanest way, but it works :slight_smile: )

// Make Geolocation optional by default
add_filter(
	'hivepress/v1/models/listing/attributes',
	function( $attributes ) {
		if ( isset( $attributes['location'] ) ) {
			$attributes['location']['edit_field']['required'] = false;
			 
		return $attributes;
	},
	1000
);

// Make Geolocation required for all categories except one
add_filter(
	'hivepress/v1/forms/listing_update',
	function( $args, $form ) {
		// Get listing
		$listing = $form->get_model();
		
		if(!$listing){
			return $args;
		}
		
		$category_ids = $listing->get_categories__id();
		
		if(!$category_ids){
			return $args;
		}

		if ( ! in_array(123, $category_ids )) { // Replace 123 by your category ID
			$args['fields']['location']['required'] = true;
		}
		
		return $args;
	},
	1000,
	2
);

Let me know if this works for you!

2 Likes

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