wordpress 我如何在woocommerce中列出最畅销的产品
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42765910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I list best selling products in woocommerce
提问by Mr Yar
I would like to consider best-selling product in this loop :
我想在这个循环中考虑最畅销的产品:
<?php
$args4 = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'cat', 'order' => 'DEC' );
$loop4 = new WP_Query( $args4 );
while ( $loop4->have_posts() ) : $loop4->the_post();?>
<div class="view-back">
<span><?php echo $product->get_categories( ) ?></span>
<span><?php echo $product->get_price_html(); ?></span>
<a class="pb" href="<?php echo get_permalink( $loop4->post->ID ) ?>">more<i class="fa fa-arrow-circle-o-right fa-2x" aria-hidden="true"></i></a>
<span class="pt"><?php the_title(); ?></span>
</div>
<?php $image4 = wp_get_attachment_image_src( get_post_thumbnail_id($loop4->post->ID), 'single-post-thumbnail' );?>
<img src="<?php echo $image4[0]; ?>" data-id="<?php echo $loop4->post->ID; ?>">
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
回答by
Solution 1:Add This plugin WP woocommerce best selling products by category
解决方案 1:按类别添加此插件WP woocommerce 畅销产品
Solution 2:For retrieving the best buying products in WooCommerce we are using wp_query manipulation with meta_key as “total_sales” and orderby “meta_value_num”. In short we are displaying products as per the total sales number.Here is the code for this
解决方案 2:为了在 WooCommerce 中检索最畅销的产品,我们使用 wp_query 操作,meta_key 为“total_sales”,orderby 为“meta_value_num”。简而言之,我们根据总销售额显示产品。这是此代码
<?php
$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 1,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product; ?>
<div>
<a href="<?php the_permalink(); ?>" id="id-<?php the_id(); ?>" title="<?php the_title(); ?>">
<?php if (has_post_thumbnail( $loop->post->ID ))
echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="product placeholder Image" width="65px" height="115px" />'; ?>
<h3><?php the_title(); ?></h3>
</a>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
回答by Oleg Apanovich
I had a hard time trying to get a list of best selling product through the standard loop. But none of the solutions from here and other stackoverflow posts with the same problem do not work for me. Finally I get result that solve my problem through the custom query to data base.
我很难通过标准循环获得最畅销产品的列表。但是这里的解决方案和其他具有相同问题的 stackoverflow 帖子都不适用于我。最后,我得到了通过对数据库的自定义查询来解决我的问题的结果。
global $wpdb;
$order_totals = $wpdb->get_results( "SELECT kp_posts.ID FROM kp_posts INNER JOIN kp_postmeta ON ( kp_posts.ID = kp_postmeta.post_id ) WHERE 1=1 AND ( kp_postmeta.meta_key = 'total_sales' ) AND kp_posts.post_type = 'product' AND (kp_posts.post_status = 'publish') GROUP BY kp_posts.ID ORDER BY kp_postmeta.meta_value+0 DESC, kp_posts.post_date DESC LIMIT 0, 16" );
foreach ( $order_totals as $value ) {
$product = wc_get_product( $value->ID );
echo $product->get_title();
}