Get vendor attribute in vendor view page

Hello,

I was creating snippets for the listing view page and the vendor view page, and I have two questions:

  1. I want to show a custom field in the vendor view page below the description on the right side. I have created a snippet code as below, but I am not sure how to get the value from demo_custom_field. For now, it just shows a custom field name. Can you please advise?
add_filter(
    'hivepress/v1/templates/vendor_view_page', 

    function($template) {	
        return hivepress()->helper->merge_trees($template, [
            'blocks' => [
                'page_content' => [
                    'blocks' => [
                        'demo_custom_field' => [
                            'type' => 'content',
                            'content' => 'demo_custom_field',
                            '_order' => 1,
                        ]
                    ]
                ]
            ]	
        ]);
    }
);
  1. I have created a snippet to show a custom field in the listing view page that will contain a WPForm shortcode. I have defined a custom field as text, but it does not show the WP form. How should I define this field so it shows the form?

image

Hi,

  1. With this hook, you can set the second parameter in the function and use $object->get_context('vendor') to get the template object.

  2. Please provide more details on how you added this shortcode to the Listing View Page.

Hi Andrii,

  1. Thanks for the hint, but I wasn’t able to make it work - returns Warning : Attempt to read property “demo_custom_field” on null.
    Could you please help me and tell me what I did wrong?
 add_filter('hivepress/v1/templates/vendor_view_page', 
 function($template, $object) {
     $vendor = $object->get_context('vendor');
         {
         return hivepress()->helper->merge_trees($template, [
             'blocks' => [
                 'page_content' => [
                     'blocks' => [
                         'demo_custom_field' => [
                             'type' => 'content',
                             'content' => $vendor->demo_custom_field,
                             '_order' => 1,
                         ]
                     ]
                 ]
             ]
         ]);
     }
 }, 10, 2);

  1. I managed to fix this snippet.

Hi,

Unfortunately, it will not work this way, if you need to get an attribute, you need to write it like this: $vendor->get_attributename()

I made changes, but it still shows an error: " Fatal error : Uncaught Error: Call to a member function get_demo_custom_field() on null in…". Could you please share a correct snippet?

 add_filter('hivepress/v1/templates/vendor_view_page', 
 function($template, $object) {
     $vendor = $object->get_context('vendor');
         {
         return hivepress()->helper->merge_trees($template, [
             'blocks' => [
                 'page_content' => [
                     'blocks' => [
                         'demo_custom_field' => [
                             'type' => 'content',
                             'content' => $vendor->get_demo_custom_field(),
                             '_order' => 1,
                         ]
                     ]
                 ]
             ]
         ]);
     }
 }, 10, 2);

I’m sorry for the delay. Please try this one instead:

add_filter(
	'hivepress/v1/templates/vendor_view_page',
	function( $template, $object ) {
		$vendor = hivepress()->request->get_context( 'vendor' );

		if ( $vendor ) {
			$template = hivepress()->helper->merge_trees(
				$template,
				[
					'blocks' => [
						'page_content' => [
							'blocks' => [
								'demo_custom_field' => [
									'type'    => 'content',
									'content' => $vendor->get_demo_custom_field(),
									'_order'  => 1,
								],
							],
						],
					],
				]
			);
		}

		return $template;
	},
	10,
	2
);
1 Like

Thank you Ihor! It works like a charm. I have an additional question regarding the demo_custom_field on the frontend. I’ve defined it as a Textarea and enabled the option to allow HTML formatting. However, when the text is entered in two rows, on the frontend, it appears as a single line. How can I fix this so that the text is displayed in the same format as it was entered in the backend?

demo_custom_field_text_format

Hi,

Unfortunately, at the moment, attributes are without line breaks, but we will try to improve this in future updates. As a workaround, we recommend using the br tag HTML br tag

It doesn’t work with <br>, the text is still in one line.

Hi,

I see. Unfortunately, there is no simple solution, but we have added this issue to the bug tracker.

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