wordpress WooCommerce:仅在商店中显示在售产品
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20990199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WooCommerce: Display ONLY on-sale products in Shop
提问by Giraldi
I need to create a products archive page(usually the Shoppage in WooCommerce) but displays ONLY the ON SALEproducts. Basically it should use the same template layoutas that in the archive-product.php
. There will be a link in the main menu that will direct to this page. How do I go about this?
我需要创建一个产品存档页面(通常是WooCommerce 中的Shop页面)但只显示ON SALE产品。基本上,它应该使用相同的模板布局为中。主菜单中将有一个指向此页面的链接。我该怎么做?archive-product.php
UPDATE
更新
I managed to filter out the ON SALEproducts with the code below placed just above the if ( have_posts() ) :
line...
我设法用下面的代码过滤掉了在线销售的产品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 );
The code is placed in a copyof archive-product.php
which I named archive-product_sale.php
and made as a page template.
代码放置在我命名并制作为页面模板的副本中。archive-product.php
archive-product_sale.php
However, this only works for Simple productstype and I need it to work for both Simpleproducts and Variableproducts type.
但是,这仅适用于简单产品类型,我需要它同时适用于简单产品和可变产品类型。
回答by helgatheviking
@mirus' answer regarding the shortcode gave me the idea to check out how WooCommerce is querying only the on-sale items. Apparently WooCommerce has a wc_get_product_ids_on_sale()
function that will return the IDs of the on-sale items. Then we can easily adjust the query using the post__in
parameter to only return those specific items.
@mirus 关于短代码的回答让我想到了检查 WooCommerce 如何仅查询在售商品。显然,WooCommerce 有一个wc_get_product_ids_on_sale()
函数可以返回在售商品的 ID。然后我们可以使用post__in
参数轻松调整查询以仅返回那些特定项目。
WooCommerce has a woocommerce_product_query
hook in the class-wc-query.php
class that allows for us to modify the query before it is run.... it is run on pre_get_posts
which is the usual place for modifying the query. Using Woo's hook just means you let them handle the majority of the conditional logic about when this query modification should be applied.
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 );
}
回答by Giraldi
I managed to filter out the ON SALEproducts with the code below placed just abovethe if ( have_posts() ) :
line...
我设法过滤掉发售的产品与下面的代码放在正上方的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 );
The code is placed in a copy of archive-product.php
which I renamed archive-product_sale.php
and made as a page template.
代码放在一个副本中archive-product.php
,我将其重命名archive-product_sale.php
并制作为页面模板。
回答by Mykola Mykolayovich Dolynskyi
@gmaggiousing query_posts() will break your site. Use pre_get_posts
@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); d($query);
}
if(isset($_GET['bestsellers'])) {
$meta_query = array(
array(
'key' => 'total_sales',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
);
}
}
return $query;
}
回答by simple-user
Solution for variable and simple products:
可变和简单产品的解决方案:
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;
}
回答by mirus
Create a new page using shortcode [sale_products per_page="12"]
使用简码创建新页面 [sale_products per_page="12"]
List of available shortcodes and their parameters is here: http://docs.woothemes.com/document/woocommerce-shortcodes/
可用简码列表及其参数在这里:http: //docs.woothemes.com/document/woocommerce-shortcodes/