【问题标题】:How can I sort best selling products in woocommerce如何在 woocommerce 中对畅销产品进行分类
【发布时间】:2022-08-17 12:48:26
【问题描述】:

我想在商店页面中对最畅销的产品进行排序 我怎样才能做到这一点?

标签: wordpress woocommerce


【解决方案1】:

利用总销售额对于'meta_key'和元值编号对于'orderby'

<?php
$args = array(
    'post_type' => 'product',
    'meta_key' => 'total_sales',
    'orderby' => 'meta_value_num',
    'posts_per_page' => 10,
);
$product_query = new WP_Query( $args );
while ( $product_query ->have_posts() ) : $product_query ->the_post();
    ?>
    <h3><?php the_title(); ?></h3>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

为了特定类别或子类别

<?php
$args = array(
    'post_type' => 'product',
    'meta_key' => 'total_sales',
    'orderby' => 'meta_value_num',
    'posts_per_page' => 10,
    'tax_query'      => array(
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'id',
            'terms'    => array($categories_id)
        ),
    )
);
$product_query = new WP_Query( $args );
while ( $product_query ->have_posts() ) : $product_query ->the_post();
    ?>
    <h3><?php the_title(); ?></h3>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

为了woocommerce 默认查询

add_filter('woocommerce_get_catalog_ordering_args','woocommerce_catalog_orderby' ); 
function woocommerce_catalog_orderby( $args ) {
    $args['meta_key']  = 'total_sales';
    $args['orderby']   = 'meta_value_num';
    return $args;
}

【讨论】:

  • 我应该把这个放在哪里?function.php?
  • 如果要在 woo-commerce 默认查询中排序,请使用此代码 add_filter('woocommerce_get_catalog_ordering_args', 'woocommerce_catalog_orderby' ); function woocommerce_catalog_orderby( $args ) { $args['meta_key'] = 'total_sales'; $args['orderby'] = 'meta_value_num'; return $args; }
  • 您如何从特定类别或子类别中获得最畅销的产品?
猜你喜欢
  • 2020-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多