使用短代码或 php 代码在 Woocommerce 中显示价格高于 99 美元的产品
Show products from above 99$ price in Woocommerce using shortcode or php code
我正在搜索基于产品价格的条件简码。我现在在 Woocommerce 上有 2k+ 产品,我想展示特定类别和整个产品中价格超过 99 美元的产品。
如何在短代码中应用此条件?有什么办法吗?
实际上,我想在小部件中申请,所以简码形式可以。
下面是简单的 shortcode
展示产品,但我想应用这个需要的条件:
[products limit="10" columns="4" category="hoodies, tshirts"]
任何帮助将不胜感激。
2018 年 8 月更新:(添加 'type' => 'DECIMAL',到 meta_query 数组)
For WooCommerce 3.3+ you can better use:
可以使用包含价格的 假类别 来调整短代码,例如您的情况:price-above-99
其中 'above'
将是运算符 '>'
和 '99'
价格金额。运算符也可以是:
price-below-20
(其中 'below'
运算符 '<'
和 '20'
价格金额)。
price-equal-25
(其中 'below'
运算符 '='
和 '25'
价格金额)。
现在您的简码 (假类别) 将是:
[products limit="10" columns="4" category="hoodies, tshirts, price-above-99"]
这里是函数的代码,将允许:
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 文件。
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( $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,
)
);
这将删除可变产品……
我正在搜索基于产品价格的条件简码。我现在在 Woocommerce 上有 2k+ 产品,我想展示特定类别和整个产品中价格超过 99 美元的产品。
如何在短代码中应用此条件?有什么办法吗?
实际上,我想在小部件中申请,所以简码形式可以。
下面是简单的 shortcode
展示产品,但我想应用这个需要的条件:
[products limit="10" columns="4" category="hoodies, tshirts"]
任何帮助将不胜感激。
2018 年 8 月更新:(添加 'type' => 'DECIMAL',到 meta_query 数组)
For WooCommerce 3.3+ you can better use:
可以使用包含价格的 假类别 来调整短代码,例如您的情况:price-above-99
其中 'above'
将是运算符 '>'
和 '99'
价格金额。运算符也可以是:
price-below-20
(其中'below'
运算符'<'
和'20'
价格金额)。price-equal-25
(其中'below'
运算符'='
和'25'
价格金额)。
现在您的简码 (假类别) 将是:
[products limit="10" columns="4" category="hoodies, tshirts, price-above-99"]
这里是函数的代码,将允许:
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 文件。
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( $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,
)
);
这将删除可变产品……