如何获得woocommerce中最受欢迎产品的类别列表

nis*_*ant 5 wordpress woocommerce

如何top 5 most popular category在我的wordpress网站主页上列出(或最受欢迎的产品类别).我使用woocommerce插件的产品.

提前感谢任何建议或解决方案.

Col*_*una 7

由于没有一个答案是作者问题的解决方案,这就是我提出的问题.这是一个简短的代码段,按类别列出了热门产品.通过流行我的意思是大多数销售的产品(如总销售额).

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' );
Run Code Online (Sandbox Code Playgroud)

您可以通过将其命名为:

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

这个短代码有一些选择:

cats :用于检索产品的类别ID或逗号分隔ID.

tax :从中获取产品的分类,默认是 product_cat

per_cat :要检索的产品数量

columns :要显示的列数

include_children :如果为false,则仅显示该类别的直接子项,如果为true,则将显示子项的子项

title :要显示的标题

link_text :链接到商店的链接文本

请注意,这个段假定你有一个名为短代码title,它需要其他一些参数,如linklink_text参数.您可以随时根据主题进行更改.

希望能帮助到你.


小智 0

我建议您查看此页面。

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"]
Run Code Online (Sandbox Code Playgroud)

或者您可以使用此插件:https://wordpress.org/plugins/sp-woocommerce-best-looking-products-by-category/