我想在这个循环中考虑最畅销的产品:
<?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(); ?>
答案 0 :(得分:13)
解决方案1:添加此插件WP woocommerce best selling products by category
解决方案2:为了在WooCommerce中检索最佳购买产品,我们使用w__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(); ?>
答案 1 :(得分:0)
我很难通过标准循环获得最畅销产品的清单。但是,这里的解决方案和其他具有相同问题的stackoverflow帖子都不适合我。 最后,我得到了通过自定义查询数据库解决我的问题的结果。
version
-------------------------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 9.6.11 on x86_64-pc-linux-gnu (Ubuntu 9.6.11-1.pgdg16.04+1), compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609, 64-bit
(1 row)
version
-----------------------------------------------------------------------------------------
PostgreSQL 9.6.10 on aarch64-suse-linux-gnu, compiled by gcc (SUSE Linux) 4.8.5, 64-bit
(1 row)
答案 2 :(得分:0)
SELECT post.post_author as user_id ,sum(meta.meta_value) as product_sell FROM wp_posts as post,wp_postmeta as meta WHERE post.post_type = 'product' AND post.ID = meta.post_id AND meta.meta_key = 'total_sales' group by post_author
答案 3 :(得分:-3)
使用插件,例如WooCommerce Product Table插件。这是一个很好的选择的原因:
此外,它真的很容易安装,并且有一个方便的教程,其中包含遵循List Popular Bestselling Products的简单逐步说明。