![](/img/trans.png)
[英]Hide products with zero or empty price from Woocommerce archives pages
[英]How to hide products with price higher than 1 on WooCommerce shop and archives pages
我正在使用此代碼在產品價格高於 1 的商店頁面上隱藏產品。
然而,沒有想要的結果。 go 哪里錯了?
我的代碼:
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'
。
'='
, '!='
, '>'
, '>='
, '<'
, '<='
, '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 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.