Display a Vendor Attribute in the vendor Block

Could you please share with me a snippet to display a vendor attribute in the listing block?

I wrote like bellow code.


add_filter(
	'hivepress/v1/templates/vendor_view_block/blocks',
	function($blocks, $template){
		$vendor = $template->get_context('vendor');
		
		if(!$vendor || !$vendor->get_categories()){
			return $blocks;
		}
		
		$output = '';
		
		foreach ( $vendor->get_categories() as $category ){
			$output .= '<span>'.esc_html( $category->get_name() ).'</span>';
		}
	
		return hivepress()->helper->merge_trees(
			[ 'blocks' => $blocks ],
			[
				'blocks' => [
					'vendor_content' => [
						'blocks' => [
							'custom_vendor_categories' => [
								'type'   => 'content',
								'content'  => $output,
								'_order' => 21,
							],
						],
					],
				],
			]
		)['blocks'];
	},
	1000,
	2
);

So I can became display categories.

And I want to display vendor’s attribute too.

How do I write code?

Please teach me.

Please try this PHP code snippet to start. But please note that it can require further customization

add_filter(
	'hivepress/v1/templates/listing_view_block/blocks',
	function($blocks, $template){
		$vendor = $template->get_context('vendor');
		
		if(!$vendor){
			return $blocks;
		}
		
		$output = '';
		
		$attributes = array_keys((array)hivepress()->attribute->get_attributes('vendor'));
		
		foreach ( $vendor->_get_fields() as $field ) {
			if ( $field->get_value() && in_array($field->get_name(), $attributes)) {
				$output .= '<div>' . $field->get_label().': '.$field->display() . '</div>';
			}
		}
		
		return hivepress()->template->merge_blocks(
			$blocks,
			[
				'listing_content' => [
					'blocks' => [
						'custom_vendor_attributes' => [
							'type' => 'content',
							'content' => $output,
							'_order' => 40,
						]
					]
				]
			]
		);
	},
	1000,
	2
);

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