簡體   English   中英

如何在 WooCommerce 商店頁面上隱藏庫存少於 2 的產品

[英]How to hide products with stock less than 2 on WooCommerce shop page

我擁有一家電子商務公司,我想隱藏庫存少於 3 件的產品。

這就是我想到的:

add_action( 'woocommerce_product_query', 'react2wp_hide_products_with_stock_higher_than_two' );
function react2wp_hide_products_with_stock_higher_than_two( $q ){
    $meta_query = $q->get( 'meta_query' );
    $meta_query[] = array(
        'key'       => '_stock',
        'value'     => 2,
        'compare'   => '>'
    );
    $q->set( 'meta_query', $meta_query );
}

你有沒有設置過類似的代碼? 這行有什么錯誤嗎?

你很接近,添加type

'type' => 'numeric' // specify it for numeric values

function react2wp_hide_products_with_stock_higher_than_two( $q, $query ) {
    // Get any existing meta query
    $meta_query = $q->get( 'meta_query' );

    // Define an additional meta query 
    $meta_query[] = array(
        'key'       => '_stock',
        'value'     => 2,
        '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_with_stock_higher_than_two', 10, 2 );

暫無
暫無

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

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