job.answiz.com
  • 4
Votes
name

How can I list out the top 5 most popular category(or category of most popular products) on my wordpress site home page. I have used woocommerce plugin for products.

Thanks in advance for any suggestion or solution.

There are 2 ways that you can list a product in multiple categories

From the Product Summary Screen

  1. Navigate to WooCommerce → Products
  2. Find the product you want to assign a category to and click “QuickEdit”
  3. Select the categories from the list
  4. Press “Update”

From the Product Post

  1. Navigate to WooCommerce → Products
  2. Find the product you want and click “Edit”
  3. On the right side of the screen you should see a section that says “Categories”
  4. Select the categories you want the product to be found under
  5. Press “Update” on the top right of the page

Hope this helps!

  • 1
Reply Report

This is very simple plugin to display woocommerce Featured product by category.

View DEMO | PRO DEMO and Features for additional information.

Check Featured Product Slider with category for additional information.

Also work with Gutenberg shortcode block.

Shortcode example :

[featured_product_categories cats="CATEGORY_ID" per_cat="6" columns="3"] 

With this plugin you can now display woocommerce best selling products by category.

FEATURES INCLUDE:

  • Featured product by category
  • Easy to configure
  • Smoothly integrates into any woocommerce theme

PRO FEATURES :

Premium Version

  • 3 shortcodes with various parameters.
    [products_slider] OR [products_slider cats="CATEGORY-ID"]
    [bestselling_products_slider] OR [bestselling_products_slider cats="CATEGORY-ID"]
    [featured_products_slider] OR [featured_products_slider cats="CATEGORY-ID"]
  • 3 Widgets.
  • 15 Designs
    View PRO DEMO and Features for additional information.

  • 1
Reply Report

Since none of the answers is a solution to the author's question, here is what I came up with. This is a shortcode snippet that lists popular products by categories. By popular I mean most sold products ( as in total sales).

function bestselling_products_by_categories( $atts ){

    global $woocommerce_loop;

    extract(shortcode_atts(array(
        'cats' => '',   
        'tax' => 'product_cat', 
        'per_cat' => '5',   
        'columns' => '5',
        'include_children' => false,
        'title' => 'Popular Products',
        'link_text' => 'See all',
    ), $atts));

    if(empty($cats)){
        $terms = get_terms( 'product_cat', array('hide_empty' => true, 'fields' => 'ids'));
        $cats = implode(',', $terms);
    }

    $cats = explode(',', $cats);

    if( empty($cats) )
        return '';

    ob_start();

    foreach($cats as $cat){

        // get the product category
        $term = get_term( $cat, $tax);

        // setup query
        $args = array(
            'post_type'             => 'product',
            'post_status'           => 'publish',
            'ignore_sticky_posts'   => 1,
            'posts_per_page'        => $per_cat,            
            'meta_key'              => 'total_sales',
            'orderby'               => 'meta_value_num',
            'tax_query' => array(               
                array(
                    'taxonomy' => $tax,
                    'field' => 'id',
                    'terms' => $cat,
                    'include_children' => $include_children,
                )
            ),
            'meta_query'            => array(
                array(
                    'key'       => '_visibility',
                    'value'     => array( 'catalog', 'visible' ),
                    'compare'   => 'IN'
                )
            )
        );

        // set woocommerce columns
        $woocommerce_loop['columns'] = $columns;

        // query database
        $products = new WP_Query( $args );

        $woocommerce_loop['columns'] = $columns;

        if ( $products->have_posts() ) : ?>

            <?php if ( shortcode_exists('title') ) : ?>
                <?php echo do_shortcode('[title text="'. $title .'" link="' . get_term_link( $cat, 'product_cat' ) . '" link_text="' . $link_text . '"]'); ?>
            <?php else : ?>
                <?php echo '<h2>'. $title .'</h2>'; ?>
            <?php endif; ?>

            <?php woocommerce_product_loop_start(); ?>

                <?php while ( $products->have_posts() ) : $products->the_post(); ?>

                    <?php woocommerce_get_template_part( 'content', 'product' ); ?>

                <?php endwhile; // end of the loop. ?>

            <?php woocommerce_product_loop_end(); ?>

        <?php endif;

        wp_reset_postdata();
    }

    return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';

} add_shortcode( 'custom_bestselling_product_by_categories', 'bestselling_products_by_categories' );

You can use this by calling it as:

<?php echo do_shortcode('[custom_bestselling_product_by_categories cats="' . $term->term_id . '"]'); ?>

This shortcode has some options:

cats : the category ID or comma-separated IDs to retrieve the products from.

tax : the taxonomy to get the products from, default is product_cat

per_cat : number of products to retrieve

columns : number of columns to display

include_children : if false only direct children of the category will be displayed, if true then children of children will be displayed

title : title to display

link_text : the link text linked to the store

Notice that this snippet assumes you have a shortcode named title and it takes a few other parameters such as link and link_text arguments. You can always change this according to your theme.

Hope it helps.

  • 4
Reply Report