Gir*_*ldi 10 wordpress product archive shop woocommerce
我需要创建一个产品存档页面(通常是WooCommerce中的Shop页面),但只显示ON SALE产品.基本上,它应该使用相同的模板布局为中.主菜单中将有一个指向此页面的链接.我该怎么做?archive-product.php
我设法过滤掉了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 );
Run Code Online (Sandbox Code Playgroud)
代码放在我命名并作为页面模板制作的副本中.archive-product.phparchive-product_sale.php
但是,这仅适用于Simple产品类型,我需要它适用于Simple产品和Variable产品类型.
hel*_*ing 14
关于短代码的@mirus回答给了我一个想法,看看WooCommerce如何只询问销售商品.显然,WooCommerce有一个wc_get_product_ids_on_sale()功能,它将返回销售商品的ID.然后我们可以使用post__in参数轻松调整查询以仅返回那些特定项.
WooCommerce woocommerce_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 );
}
Run Code Online (Sandbox Code Playgroud)
我设法过滤掉发售的产品与下面的代码放在正上方的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 );
Run Code Online (Sandbox Code Playgroud)
代码放在archive-product.php我重命名archive-product_sale.php并作为页面模板制作的副本中.
小智 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); d($query);
}
if(isset($_GET['bestsellers'])) {
$meta_query = array(
array(
'key' => 'total_sales',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
);
}
}
return $query;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37492 次 |
| 最近记录: |