How to get a list of categories of the most popular products in woocommerce

How can I list top 5 most popular category(or a category of the most popular products) on my home page wordpress. I used the plugin woocommercefor products.

Thanks in advance for any suggestion or solution.

+4
source share
3 answers

Since none of the answers is a solution to the author’s question, this is what I came up with. This is a short code snippet that lists popular products by category. By popularity, I mean the majority of products sold (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 naming it as:

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

There are several options in this short code:

cats: , , .

tax: , product_cat

per_cat:

columns:

include_children: false, , true,

title:

link_text: ,

, , title, , link link_text . .

, .

+4

, , , . . , , .

$query_args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'ignore_sticky_posts' => 1,
        'posts_per_page' => '10',
        'columns' => '4',
        'fields' => 'ids',
        'meta_key' => 'total_sales',
        'orderby' => 'meta_value_num',
        'meta_query' => WC()->query->get_meta_query()
    );

    $best_sell_products_query = query_posts($query_args);
    return $best_sell_products_query;
+1

I recommend that you check this page.

http://docs.woothemes.com/document/woocommerce-shortcodes/

array(
     'per_page' => '12',
      'columns' => '4',
      'orderby' => 'title',
      'order' => 'asc',
      'category' => ''
 )
[product_category category="appliances"]


array(
     'per_page' => '12',
     'columns' => '4',
     'orderby' => 'title',
     'order' => 'asc'
 )

[top_rated_products per_page="12"]

Or you can use this plugin: https://wordpress.org/plugins/sp-woocommerce-best-selling-products-by-category/

0
source

All Articles