使用短代码或php代码在Woocommerce中显示价格超过99 $的产品 [英] Show products from above 99$ price in Woocommerce using shortcode or php code
问题描述
我正在根据产品价格搜索有条件的简码.我现在在Woocommerce上有2k多种产品,并且我想显示特定类别和整个产品中价格高于99 $的产品.
I am searching for a conditional shortcode based on product price. I have 2k+ products on Woocommerce now and I want to show products above 99$ price from a specific category and from whole products.
如何在简码中应用此条件?有什么办法吗?
How can I apply this conditions in the shortcode? Is there any way to do this?
实际上,我想在小部件中应用,因此简码形式可以使用.
Actually, I want to apply in a widget, so shortcode form will work.
下面是显示产品的简单shortcode
,但我想应用此必要条件:
Below is simple shortcode
to show products but I want to apply this needed condition:
[products limit="10" columns="4" category="hoodies, tshirts"]
任何帮助将不胜感激.
推荐答案
2018年8月更新:(已将'type'=>'DECIMAL'添加到meta_query数组)
Update on August 2018: (added 'type' => 'DECIMAL', to the meta_query array)
对于WooCommerce 3.3+,您可以更好地使用:
在页面上显示Woocommerce中所有价格低于特定价格的产品
For WooCommerce 3.3+ you can better use:
Display on a page all products below a specific price in Woocommerce
这可以用包含以下价格的假类别来调整简码:price-above-99
其中'above'
将是运算符'>'
和 '99'
价格金额.运算符也可以是:
This is possible tweaking the shortcode with a fake category that contain the price like for your case: price-above-99
where 'above'
will be the operator '>'
and '99'
the price amount. The operator can also be:
-
price-below-20
(其中'below'
操作员'<'
和'20'
价格金额). -
price-equal-25
(其中'below'
操作员'='
和'25'
价格金额).
price-below-20
( where'below'
the operator'<'
and'20'
the price amount ).price-equal-25
( where'below'
the operator'='
and'25'
the price amount ).
现在,您的简码(带有假类别)将为:
[products limit="10" columns="4" category="hoodies, tshirts, price-above-99"]
下面是该函数的代码,其允许您执行以下操作:
Here is the code of the function that will allow that:
add_filter( 'woocommerce_shortcode_products_query', 'shortcode_products_price', 10, 3 );
function shortcode_products_price( $query_args, $atts, $loop_name ) {
$has_price = false;
$compare = $price = '';
$categories = $atts['category'];
$categories = str_replace(' ', '', $categories);
$categories_array = explode(',', $categories);
if( count($categories_array) > 0 )
foreach( $categories_array as $category )
if (strpos($category, 'price-') !== false) {
$has_price = true;
$values = explode('-', $category);
if( 'above' == $values[1] ) $compare = '>';
elseif( 'below' == $values[1] ) $compare = '<';
else $compare = '=';
$price = $values[2];
}
if( $has_price )
$query_args['meta_query'][] = array(
'key' => '_price',
'value' => $price,
'type' => 'DECIMAL', // <== Updated
'compare' => $compare,
);
return $query_args;
}
代码进入活动子主题(或活动主题)的function.php文件.
Code goes in function.php file of the active child theme (or active theme).
它在简单产品上的运行效果很好,但是对于可变产品,由于它们的价格很高,因此价格最高(在您的情况下)...
It works quiet well on simple products, but for variable products, as they have many prices, it will take the highest price (in your case)...
如果您要排除可变产品,则将元查询数组 $query_args['meta_query']
替换为:
If you want to exclude variable products you will replace the meta query array $query_args['meta_query']
by this:
if( $has_price )
$query_args['meta_query'][] = array(
'relation' => 'OR',
array(
'key' => '_regular_price',
'value' => $price,
'type' => 'DECIMAL', // <== Updated
'compare' => $compare,
),
array(
'key' => '_sale_price',
'value' => $price,
'type' => 'DECIMAL', // <== Updated
'compare' => $compare,
)
);
这将删除变量产品……
这篇关于使用短代码或php代码在Woocommerce中显示价格超过99 $的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!