3

For this question, I know how to solve it but I am hoping there is a better / faster way to solve it.

I have a custom meta query in my product loop that doesn't display my product if the stock is 0, but I only want to do this when backorder is set to no on a product. Is this possible without running 2 queries (one that gets all the posts and checks if backorders are enabled, and one that only executes the meta query if backorders are not enabled)?

Here is my custom meta query in my functions.php:

add_action( 'woocommerce_product_query', 'woo_custom_outofstock_query' );

function woo_custom_outofstock_query( $q ) {
    $meta_query = $q->get( 'meta_query' );

    $meta_query[] = array(
        'key'       => '_stock',
        'value'     => '0.000000',
        'compare'   => 'NOT IN'
    );

    $q->set( 'meta_query', $meta_query );
}

EDIT: I changed the query to:

$meta_query = $q->get( 'meta_query' );

$meta_query[] = array(
    'key'       => '_stock',
    'value'     => '0.000000',
    'compare'   => 'NOT IN'
);

$meta_query[] = array(
    'key'       => '_backorders',
    'value'     => 'no',
    'compare'   => '!='
);

$q->set( 'meta_query', $meta_query );

This should do the trick but for some reason it is still not working. Also, when I hide out of stock products in the WooCommerce settings, it still doesn't display products with a stock of 0 when backorders are enabled.

1 Answer 1

3

The default relation for a meta query is AND, so when you have two conditions they will be treated as such:

$meta_query[] = array(
    'key'       => '_stock',
    'value'     => '0.000000',
    'compare'   => 'NOT IN'
);

$meta_query[] = array(
    'key'       => '_backorders',
    'value'     => 'no',
    'compare'   => '!='
);

Get all products with a _stock meta value NOT IN 0.000000 AND _backorders NOT EQUAL TO no

https://codex.wordpress.org/Class_Reference/WP_Meta_Query

9
  • Exactly what I'm talking about ;-). This question deserves an answer Mar 2, 2016 at 9:37
  • Thanks, I have tried this already but it still wouldn't display the product if backorders was yes and the stock is 0.
    – Jascha
    Mar 2, 2016 at 9:43
  • @PieterGoosen I agree you could also just see this as a question about meta_queries so that wouldn't make it off topic.
    – Jascha
    Mar 2, 2016 at 9:46
  • Have you checked the Out Of Stock Visibility checkbox in Woocommerce -> Settings -> Products -> Inventory ? That will prevent out of stock products showing.
    – Adam
    Mar 2, 2016 at 9:56
  • Wow this solved it indeed, I thought that this only worked when a product is set to Sold out, It seems i was thinking way too complicated for an uncomplicated situation. Thanks! @userabuser
    – Jascha
    Mar 2, 2016 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.