简体   繁体   English

如何在 WooCommerce 商店页面上隐藏库存少于 2 的产品

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

I own an e-Commerce business and i want to hide the products that have a stock of less than 3.我拥有一家电子商务公司,我想隐藏库存少于 3 件的产品。

This is what i came up to:这就是我想到的:

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

Have you ever set a similar code?你有没有设置过类似的代码? Any errors in this line?这行有什么错误吗?

You're close, add type你很接近,添加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