簡體   English   中英

如何在WooCommerce商店和檔案頁面隱藏價格高於1的產品

[英]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'


  • 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 );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM