首页 文章

WooCommerce:仅在商店中展示销售产品

提问于
浏览
9

我需要创建 products archive page (通常是 WooCommerce 中的 Shop 页面),但显示 ONLY the ON SALE products . 基本上它应该使用与 archive-product.php 中相同的 template layout . 主菜单中将有一个指向此页面的链接 . 我该怎么做?

更新

我设法过滤掉 ON SALE 产品,下面的代码位于 if ( have_posts() ) : 线上方......

$args = array(
    'post_type'      => 'product',
    'order'          => 'ASC',
    'paged'          => $paged,
    'meta_query'     => array(
        array(
            'key'           => '_sale_price',
            'value'         => 0,
            'compare'       => '>',
            'type'          => 'numeric'
        )
    )
);

query_posts( $args );

代码放在 copyarchive-product.php 中,我将其命名为 archive-product_sale.php 并作为 page template .

但是,这仅适用于 Simple products 类型,我需要它适用于 Simple 产品和 Variable 产品类型 .

4 回答

  • 5

    关于短代码的@mirus回答给了我一个想法,看看WooCommerce如何只询问销售商品 . 显然,WooCommerce有一个 wc_get_product_ids_on_sale() 函数,它将返回销售商品的ID . 然后我们可以使用 post__in 参数轻松调整查询,仅返回这些特定项目 .

    WooCommerce在 class-wc-query.php 类中有一个 woocommerce_product_query 钩子,它允许我们在运行之前修改查询....它在 pre_get_posts 上运行,这是修改查询的通常位置 . 使用Woo的钩子只意味着让它们处理大多数条件逻辑关于应该应用此查询修改的时间 .

    add_action( 'woocommerce_product_query', 'so_20990199_product_query' );
    
    function so_20990199_product_query( $q ){
    
        $product_ids_on_sale = wc_get_product_ids_on_sale();
    
        $q->set( 'post__in', $product_ids_on_sale );
    
    }
    
  • 0

    我设法过滤掉 ON SALE 产品的代码 placed just above if ( have_posts() ) : 行...

    $args = array(
        'post_type'      => 'product',
        'meta_query'     => array(
            'relation' => 'OR',
            array( // Simple products type
                'key'           => '_sale_price',
                'value'         => 0,
                'compare'       => '>',
                'type'          => 'numeric'
            ),
            array( // Variable products type
                'key'           => '_min_variation_sale_price',
                'value'         => 0,
                'compare'       => '>',
                'type'          => 'numeric'
            )
        )
    );
    
    query_posts( $args );
    

    代码放在 archive-product.php 的副本中,我将其重命名为 archive-product_sale.php 并制作为 page template .

  • 13

    @gmaggio使用query_posts()will break your site . 使用pre_get_posts

    add_filter( 'pre_get_posts', 'catalog_filters' );
    function catalog_filters( $query ) {
        if ( $query->is_main_query() && $query->post_type = 'product' ) {
            if(isset($_GET['onsale'])) {
                $meta_query = array(
                    'relation' => 'OR',
                    array( // Simple products type
                    'key' => '_sale_price',
                    'value' => 0,
                    'compare' => '>',
                    'type' => 'numeric'
                    ),
                    array( // Variable products type
                    'key' => '_min_variation_sale_price',
                    'value' => 0,
                    'compare' => '>',
                    'type' => 'numeric'
                    )
                ); $query->set('meta_query', $meta_query); d($query);
            }
            if(isset($_GET['bestsellers'])) {
                $meta_query     = array(
                array( 
                    'key'           => 'total_sales',
                    'value'         => 0,
                    'compare'       => '>',
                    'type'          => 'numeric'
                    )
                );
            }
        }
    
    return $query;
    }
    
  • 9

    使用短代码创建新页面 [sale_products per_page="12"]

    可用的短代码及其参数列表如下:http://docs.woothemes.com/document/woocommerce-shortcodes/

相关问题