Hide primary listing attributes for specific categories

how do i limit the functionality of a snippet to a specific category? for example how do i limit the below snippet to [‘categories’] = [57];

add_filter(
	'hivepress/v1/templates/listing_view_page',
	function($template){
		return hivepress()->helper->merge_trees(
			$template,
			[
				'blocks' => [
					'listing_attributes_primary' => [
						'type' => 'content',
					],
				],
			]
		);
	},
	1000
);

Hi,

Please provide more details on why you need to hide primary attributes for a category and we will try to help. Also, as a workaround, without using the code, you can restrict the attribute for categories, then when you select another category, this attribute will not be displayed.

Thanks, well I have a few snippits in mind that i want to limit to spesific categories which is why i was wondering if theres a similar process with limiting them to categories no matter the snipit?

If so please let me know the general method used for this but I will show you the below example as one i am trying to limit to a category if it helps, the following code is to hide the price calculator, but i want it to work just for one specified category, please let me know what to add in to it to limit it to a spesific category: (i still want it showing the normal price / day on the other categories)


add_filter(
	'hivepress/v1/models/listing/fields',
	function( $fields, $model ) {
		if ( isset( $fields['price'] ) ) {
			// Replaces the price with the word "Gift"
			$fields['price']['display_template'] = 'Gift';
		}
		return $fields;
	},
	1000,
	2
);

this is my unsuccessful attempt of limiting the code to a spesific category below: (it just changes for all categories still, please help, thanks.)

add_filter(
	'hivepress/v1/models/listing/fields',
	function( $fields, $model ) {
		if ( isset( $fields['price'] ) ) {
			// Replaces the price with the word "Gift"
			$fields['price']['display_template'] = 'Gift';
			$fields['price']['categories'] = [56];
		}
		return $fields;
	},
	1000,
	2
);

I know its possible because you guys made one that works for the visibility of price attributes based on specific categories and so i tried to copy the way it worked on mine but unsuccessfully:

add_filter(
	'hivepress/v1/models/listing/attributes',
	function( $attributes ) {
		// Hide the "Price" attribute for category ID 57
		if ( isset($attributes['price']) ) {
			$attributes['price']['categories'] = [55,56,57,58];
		}
		
		// Hide the "Extras" attribute for category ID 57
		if ( isset($attributes['price_extras']) ) {
			$attributes['price_extras']['categories'] = [55,56,57,58];
		}

		return $attributes;
	},
	1000
);

If this snippet works for all categories you can try limiting it by adding another condition via the $model->get_categories__id(), it should contain an array of category ID. If it’s not, then at least $model->get_id() is available for sure and you can get category IDs this way:

wp_get_post_terms( $model->get_id(), 'hp_listing_category', array( 'fields' => 'ids' ) );

And check if the category IDs match the ones where the “Gift” display format should be used.

Thanks, with the help of chat gpt ive tried to implement those instructions but im lost,
could you tell me where to put those code snippits in my code snippit for my category id 56? here is my current snippet that works for all categories at the moment:

add_filter(
	'hivepress/v1/models/listing/fields',
	function( $fields, $model ) {
		if ( isset( $fields['price'] ) ) {
			// Replaces the price with the word "Gift"
			$fields['price']['display_template'] = 'Gift';
		}
		return $fields;
	},
	1000,
	2
);

You can try this one, but please note that it’s not tested:

add_filter(
	'hivepress/v1/models/listing/fields',
	function( $fields, $model ) {
		if ( isset( $fields['price'] ) ) {
			$category_ids=wp_get_post_terms( $model->get_id(), 'hp_listing_category', array( 'fields' => 'ids' ) );

			if(in_array(56, $category_ids)) {
				// Replaces the price with the word "Gift"
				$fields['price']['display_template'] = 'Gift';
			}
		}
		return $fields;
	},
	1000,
	2
);

Please consider hiring a developer if further code customizations are needed Experts | HivePress While we can provide general guidance, customizations of the existing functionality are beyond the support scope.

1 Like

thanks so much, it works!

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