Add specific HTML to templates depending on vendor attributes

Hey!

I use vendor attributes to categorize vendors like “commercial vendor”, “moderator” etc.

I like the way the featured listings are displayed with the border and the icon and now I would like to extend this to my other user groups.

I therfore need a way of setting CSS classes in the listing view templates based on the selected vendor attributes.

Is there an easy way of getting the vendor attributes or checking, if for the listing, there is one specific attribute set?

I used the following code to to get a true/false output in the vendor templates:

<?php
if ( $vendor->get_gewerblicher_verkaeufer() ) :
    ?>
    <span>Attribute available</span>
    <?php
endif;
?>

To use this in listing templates, is this a good solution? I just totally guessed the get_vendor() part :smiley: I have very litte understanding of PHP.

<?php
if ( $listing->get_vendor()->get_gewerblicher_verkaeufer() ) :
    ?>
    <span>Attribute available</span>
    <?php
endif;
?>

But anyways, how can I set the conteiner class accordingly then? Like so:

.hp-listing .hp-listing--view-block .hp-listing--feature .my_custom_class

Kind regards,
Johannes

Hi,

Yes, but this depends on how these HTML parts are added to the vendor template, have you inserted them via the template filter hooks? If listing or vendor objects are available in the template context then the snippets you suggested should be ok.

I tried adding them via filter hooks in some template parts I already created and they work fine, but I’m not sure on how to add the desired class to the listing container.

Or at least I don’t know how to use the filter hook with my if statement as the variables are not available in the functions.php directly.

Please try this PHP snippet to add custom class to the listing block container

add_filter(
	'hivepress/v1/templates/listing_view_block',
	function($template){
		return hivepress()->template->merge_blocks(
			$template,
			[
				'listing_container' => [
					'attributes' => [
						'class' => ['test-custom-class'],
					],
				],
			],
		);
	},
	1000
);

Thank you Yevhen, I know how to add custom classes this way, that is not my main problem. The problem is how to include the if statement, I mentioned above, in your snippet. Or in detail, how to check, if the vendor has a specific attribute set before adding the class to the listing container.

Please try this PHP snippet as an example of how to get a vendor attribute in the listing template and use it as a condition

add_filter(
	'hivepress/v1/templates/listing_view_block/blocks',
	function($blocks, $template){
		
		// Get listing.
		$listing = $template->get_context('listing');
		
		if(!$listing){
			return $blocks;
		}
			
		// Get vendor.
		$vendor = $listing->get_vendor();
		
		if(!$vendor){
			return $blocks;
		}
		
		if($vendor->get_your_attribute_field()){
			// if vendor has some attribute value.
		}else{
			// if vendor does not have some attribute value.
		}
		
		return $blocks;
	},
	1000
);

Thank you! Now I understand how this works inside the functions.php.

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