如何在 WooCommerce 商店和档案页面上隐藏价格高于 1 的产品
How to hide products with price higher than 1 on WooCommerce shop and archives pages
我正在使用此代码在产品价格高于 1 的商店页面上隐藏产品。
然而,没有得到想要的结果。哪里出错了?
我的代码:
add_action( 'woocommerce_product_query', 'react2wp_hide_products_higher_than_1' );
function react2wp_hide_products_higher_than_1( $q ){
if ( is_shop() ) {
$meta_query = $q->get( 'meta_query' );
$meta_query[] = array(
'key' => '_price',
'value' => 1,
'compare' => '>'
);
}
$q->set( 'meta_query', $meta_query );
}
- 你很接近,添加
type
'type' => 'numeric' // specify it for numeric values
type (string) - 自定义字段类型。可能的值为 'NUMERIC'
、'BINARY'
、'CHAR'
、'DATE'
、'DATETIME'
、'DECIMAL'
、'SIGNED'
、'TIME'
, 'UNSIGNED'
。默认值为 'CHAR'
.
- compare (string) - 要测试的运算符。可能的值为
'='
、'!='
、'>'
、'>='
、'<'
、'<='
、'LIKE'
、'NOT LIKE'
, 'IN'
, 'NOT IN'
, 'BETWEEN'
, 'NOT BETWEEN'
, 'EXISTS'
(仅适用于 WP >= 3.5)和 'NOT EXISTS'
(也仅适用于 WP >= 3.5).在 WordPress 3.7 中添加了值 'REGEXP'
、'NOT REGEXP'
和 'RLIKE'
。默认值为 '='
.
结果:
这将在产品存档页面(商店)上隐藏价格高于 1 的所有产品
function react2wp_hide_products_higher_than_1( $q, $query ) {
// Returns true when on the product archive page (shop).
if ( is_shop() ) {
// Get any existing meta query
$meta_query = $q->get( 'meta_query' );
// Define an additional meta query
$meta_query[] = array(
'key' => '_price',
'value' => 1,
'type' => 'numeric', // specify it for numeric values
'compare' => '<'
);
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
}
add_action( 'woocommerce_product_query', 'react2wp_hide_products_higher_than_1', 10, 2 );
我正在使用此代码在产品价格高于 1 的商店页面上隐藏产品。
然而,没有得到想要的结果。哪里出错了?
我的代码:
add_action( 'woocommerce_product_query', 'react2wp_hide_products_higher_than_1' );
function react2wp_hide_products_higher_than_1( $q ){
if ( is_shop() ) {
$meta_query = $q->get( 'meta_query' );
$meta_query[] = array(
'key' => '_price',
'value' => 1,
'compare' => '>'
);
}
$q->set( 'meta_query', $meta_query );
}
- 你很接近,添加
type
'type' => 'numeric' // specify it for numeric values
type (string) - 自定义字段类型。可能的值为 'NUMERIC'
、'BINARY'
、'CHAR'
、'DATE'
、'DATETIME'
、'DECIMAL'
、'SIGNED'
、'TIME'
, 'UNSIGNED'
。默认值为 'CHAR'
.
- compare (string) - 要测试的运算符。可能的值为
'='
、'!='
、'>'
、'>='
、'<'
、'<='
、'LIKE'
、'NOT LIKE'
,'IN'
,'NOT IN'
,'BETWEEN'
,'NOT BETWEEN'
,'EXISTS'
(仅适用于 WP >= 3.5)和'NOT EXISTS'
(也仅适用于 WP >= 3.5).在 WordPress 3.7 中添加了值'REGEXP'
、'NOT REGEXP'
和'RLIKE'
。默认值为'='
.
结果:
这将在产品存档页面(商店)上隐藏价格高于 1 的所有产品
function react2wp_hide_products_higher_than_1( $q, $query ) {
// Returns true when on the product archive page (shop).
if ( is_shop() ) {
// Get any existing meta query
$meta_query = $q->get( 'meta_query' );
// Define an additional meta query
$meta_query[] = array(
'key' => '_price',
'value' => 1,
'type' => 'numeric', // specify it for numeric values
'compare' => '<'
);
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
}
add_action( 'woocommerce_product_query', 'react2wp_hide_products_higher_than_1', 10, 2 );