在页面上显示 Woocommerce 中低于特定价格的所有产品
Display on a page all products below a specific price in Woocommerce
我有一个拥有 1000 多种产品的 Woocommerce 商店。我希望所有价格低于 999 的产品都应该显示在单独的页面上,这样我就可以在我的菜单中标记该页面。
可能吗?
- 创建新页面模板
- 创建新页面,分配新页面模板
- 在页面模板代码(或使用过滤器)之上,使用WP_query查询您的产品
参见:
$query = new \WP_Query(
[
'posts_per_page' => -1,
'post_type' => 'product',
'meta_key' => '_price',
'meta_value' => 999,
'meta_compare' => '<',
'meta_type' => 'NUMERIC'
]);
- 然后您可以使用 while 循环或
$query->posts
上的 foreach 来显示您的 post
更新: (添加 'type' => 'DECIMAL',
到 meta_query
数组)
这可以通过在页面上使用 Woocommerce shortcode [products]
来完成,并添加以下附加代码 (这将添加通过现有参数定义要比较的价格的可能性):
add_filter( 'woocommerce_shortcode_products_query', 'products_based_on_price', 10, 3 );
function products_based_on_price( $query_args, $atts, $loop_name ) {
if( ! ( isset($atts['class']) && ! empty($atts['class']) ) )
return $query_args;
if (strpos($atts['class'], 'below-') !== false) {
$compare = '<';
$slug = 'below-';
} elseif (strpos($atts['class'], 'above-') !== false) {
$compare = '>'; //changed from "<" to ">"
$slug = 'above-';
}
if( isset($compare) ) {
$query_args['meta_query'][] = array(
'key' => '_price',
'value' => (float) str_replace($slug, '', $atts['class']),
'type' => 'DECIMAL',
'compare' => $compare,
);
}
return $query_args;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
##用法:
Here we use the unused class
argument to pass the price and the comparison operator.
1) 展示低于特定金额的产品 (您的案例)
您将粘贴以下短代码示例作为 class
参数值 below-999
(对于价格低于 999 的产品):
[products limit="16" paginate="true" columns="4" class="below-999"]
wordpress页面文本内容编辑器:
您将获得:
2) 显示超过特定金额的产品
您将粘贴以下短代码示例作为 class
参数值 above-50
(对于价格高于 [ 的产品) =21=]):
[products limit="16" paginate="true" columns="4" class="above-50"]
可用的简码参数和设置:Woocommerce shortcodes documentation
我有一个拥有 1000 多种产品的 Woocommerce 商店。我希望所有价格低于 999 的产品都应该显示在单独的页面上,这样我就可以在我的菜单中标记该页面。
可能吗?
- 创建新页面模板
- 创建新页面,分配新页面模板
- 在页面模板代码(或使用过滤器)之上,使用WP_query查询您的产品
参见:
$query = new \WP_Query(
[
'posts_per_page' => -1,
'post_type' => 'product',
'meta_key' => '_price',
'meta_value' => 999,
'meta_compare' => '<',
'meta_type' => 'NUMERIC'
]);
- 然后您可以使用 while 循环或
$query->posts
上的 foreach 来显示您的 post
更新: (添加 'type' => 'DECIMAL',
到 meta_query
数组)
这可以通过在页面上使用 Woocommerce shortcode [products]
来完成,并添加以下附加代码 (这将添加通过现有参数定义要比较的价格的可能性):
add_filter( 'woocommerce_shortcode_products_query', 'products_based_on_price', 10, 3 );
function products_based_on_price( $query_args, $atts, $loop_name ) {
if( ! ( isset($atts['class']) && ! empty($atts['class']) ) )
return $query_args;
if (strpos($atts['class'], 'below-') !== false) {
$compare = '<';
$slug = 'below-';
} elseif (strpos($atts['class'], 'above-') !== false) {
$compare = '>'; //changed from "<" to ">"
$slug = 'above-';
}
if( isset($compare) ) {
$query_args['meta_query'][] = array(
'key' => '_price',
'value' => (float) str_replace($slug, '', $atts['class']),
'type' => 'DECIMAL',
'compare' => $compare,
);
}
return $query_args;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
##用法:
Here we use the unused
class
argument to pass the price and the comparison operator.
1) 展示低于特定金额的产品 (您的案例)
您将粘贴以下短代码示例作为 class
参数值 below-999
(对于价格低于 999 的产品):
[products limit="16" paginate="true" columns="4" class="below-999"]
wordpress页面文本内容编辑器:
您将获得:
2) 显示超过特定金额的产品
您将粘贴以下短代码示例作为 class
参数值 above-50
(对于价格高于 [ 的产品) =21=]):
[products limit="16" paginate="true" columns="4" class="above-50"]
可用的简码参数和设置:Woocommerce shortcodes documentation