• Resolved chiragagarwal

    (@chiragagarwal)


    Hi,

    Wondering if there is a way (such as widget or custom function) to display a Vendor’s top-rated products on their own product’s sidebar? I see there is a widget available to show vendor’s top rated products on vendor’s shop page, but it wouldn’t show on vendor’s product page. Any workaround will be appreciated!

Viewing 5 replies - 1 through 5 (of 5 total)
  • @chiragagarwal, thanks for the query.

    We do have a widget called “Products by Rating”, this displays the vendor’s top rated product on their shop page.

    Let us know if you need any further help.

    Thread Starter chiragagarwal

    (@chiragagarwal)

    Hi,

    My apologies for lack of clarity in the question. I’d like to display the “Product by Rating” widget on Product page associated with a Vendor (in addition to the Shop page). Is this possible?

    @chiragagarwal, currently the vendor’s top product widget gets displayed on vendor’s shop page only.

    However, you can do custom code and add this in the single product page. For this, please do check our code and apply the same logic in the simple product page too.

    Let us know if you need any further help.

    Thread Starter chiragagarwal

    (@chiragagarwal)

    Thanks! If it’s not too much to ask, can you suggest the code to display this widget on product page?

    @chiragagarwal, to do so first of all you have to create a custom widget for it, because our above widget is restricted to vendor shop page only. To create a custom widget, please follows this link.

    After successfully created your widget, just replace your class widget’s widget() function with follows-

    public function widget( $args, $instance ) {
                
                $query_args = array(
                        'posts_per_page' => -1,
                        'no_found_rows'  => 1,
                        'author__in'     => array( 2 ),
                        'post_status'    => 'publish',
                        'post_type'      => 'product',
                        'meta_key'       => '_wc_average_rating',
                        'orderby'        => 'meta_value_num',
                        'order'          => 'DESC',
                        'meta_query'     => WC()->query->get_meta_query(),
                        'tax_query'      => WC()->query->get_tax_query(),
                ); // WPCS: slow query ok.
    
                $r = new WP_Query( $query_args );
    
                if ( $r->have_posts() ) {
                        echo "My Widget Title"; // Title displayed on sidebar
    
                        echo wp_kses_post( apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ) );
    
                        $template_args = array(
                                'widget_id'   => $args['widget_id'],
                                'show_rating' => true,
                        );
    
                        while ( $r->have_posts() ) {
                                $r->the_post();
                                wc_get_template( 'content-widget-product.php', $template_args );
                        }
    
                        echo wp_kses_post( apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ) );
                }
    
                wp_reset_postdata();
    
                $content = ob_get_clean();
    
                echo $content; // WPCS: XSS ok.
    
    	}

    Where author__in array contain vendor ID (Ex. 2), you can replace it with multiple.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Show Vendor’s top-rated products on Product Sidebar’ is closed to new replies.