Listings count in vendor block footer

Hi there, I am using the following code to indicate the number of listings a vendor has published in the vendor footer. It shows the correct number everywhere on the website except on a single listings page! Could you kindly explain how to modify the code in order to show the correct count everywhere? Many thanks in advance!

add_filter(
    'hivepress/v1/templates/vendor_view_block',
    function( $template ) {
       
        global $post;
        $vendor_id = isset( $post->ID ) ? $post->ID : 0;

        if ( $vendor_id ) {
            
            $listing_count = HivePress\Models\Listing::query()
                ->filter([
                    'vendor' => $vendor_id,
                ])
                ->get_count();

            
            $entry_text = $listing_count === 1 ? 'Eintrag' : 'Einträge';

            
            $listing_count_html = '<div class="hp-vendor__listing-count">' . esc_html( $listing_count ) . ' ' . esc_html( $entry_text ) . '</div>';

            
            $template = hivepress()->helper->merge_trees(
                $template,
                [
                    'blocks' => [
                        'vendor_footer' => [
                            'blocks' => [
                                'custom_vendor_listing_count' => [
                                    'type'    => 'content',
                                    'content' => $listing_count_html,
                                    '_order'  => 5,
                                ],
                            ],
                        ],
                    ],
                ]
            );
        }

        return $template;
    },
    1000
);

Hi,

We recommend using hivepress/v1/templates/vendor_view_block/blocks hook, there are two parameters, the first is blocks and the other is a template object from which you can get the vendor as follows: $vendor=$template->get_context('vendor'). Please note that in the snippet you provided, the issue is that the curent post ID is taken globally, and on the listing page it is the listing, not the current vendor, because the template object will always be the current vendor.

Thank you very much, andrii! Very helpful. The counter works correctly now, however, I just can’t move it in the footer (next to the send message icon) of the vendors container, exactly as it appears in the RentalHive template. What is the right approach?

Hi,

You can overwrite the template parts of /listing/view/block/listing-attributes-primary.php using this document How to override template parts - HivePress Help Center and show it next to the primary attributes there. Or, another option is to use the template hook on listing_view_block to add a wrapper around the primary attributes.

Thank you very much for your support!