如何在woocommerce的顶级产品中添加“畅销书”的类名?
是否有检查产品是否畅销的功能?
答案 0 :(得分:0)
我做到了。这是解决方案; 首先,我生成了一个查询,它获得了10个具有最大total_sales的产品
$query = "SELECT ID FROM {$wpdb->posts} p
INNER JOIN {$wpdb->postmeta} pm ON ( pm.post_id = p.ID AND pm.meta_key='total_sales' )
WHERE p.post_type = 'product'
AND p.post_status = 'publish'
ORDER BY pm.meta_value+0 DESC
LIMIT 10";
$topselled_ids = $wpdb->get_results( $wpdb->prepare($query,OBJECT ));
//print_r ($topselled_ids );
foreach ($topselled_ids as $one_topselled_id)
$topselled[] = $one_topselled_id->ID;
然后我检查了当前产品的id是否在数组中(在content-product.php中)。
if ( in_array($post->ID, $topselled) )
$classes[] = "top_selled";