16

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

更新

我设法过滤掉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 );

代码放置在我命名的副本中,并作为页面模板制作。archive-product.phparchive-product_sale.php

但是,这仅适用于简单产品类型,我需要它同时适用于简单产品和可变产品类型。

4

5 回答 5

17

@mirus 关于简码的回答让我想到了查看 WooCommerce 如何仅查询打折商品。显然 WooCommerce 有一个wc_get_product_ids_on_sale()函数可以返回打折商品的 ID。然后我们可以使用参数轻松调整查询post__in以仅返回那些特定项目。

WooCommercewoocommerce_product_query在类中有一个钩子class-wc-query.php,允许我们在运行之前修改查询.... 它运行在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 );

}
于 2015-01-17T23:25:55.350 回答
11

我设法过滤掉ON SALE产品,并将下面的代码放在该行的正上方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并作为页面模板制作。

于 2014-01-12T10:16:38.057 回答
6

@gmaggio使用 query_posts()会破坏你的网站。使用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);
        }
        if(isset($_GET['bestsellers'])) {
            $meta_query     = array(
            array( 
                'key'           => 'total_sales',
                'value'         => 0,
                'compare'       => '>',
                'type'          => 'numeric'
                )
            );
        }
    }

return $query;
}
于 2015-05-31T19:22:02.330 回答
1

可变和简单产品的解决方案:

add_action( 'save_post_product', 'update_product_set_sale_cat_var' );

function update_product_set_sale_cat_var( $post_id ) {

    $sales_ids = wc_get_product_ids_on_sale();

    foreach ( $sales_ids as $sale_id ) :
        if ($sale_id == $post_id) :
            wp_set_object_terms($post_id, 'sale', 'product_cat', true );
        else :
            if ( has_term( 'sale', 'product_cat', $post_id ) ) {
                wp_remove_object_terms( $post_id, 'sale', 'product_cat' );
            }
        endif;
    endforeach; 

}
于 2019-10-04T09:46:31.350 回答
0

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

可用简码列表及其参数在这里:http ://docs.woothemes.com/document/woocommerce-shortcodes/

于 2014-01-08T12:38:09.140 回答