NO BUG: Problem in sorting products based on stock quantity

Browser: Chrome new
OS: macOS / Windows / Linux / etc.
URL: Link to a page that illustrates this issue

Hi,

If we use {woo_product_stock:value} to display the stock quantity in queru loop, it will display the total number of variables in the variable products. Now, if we display the order of products based on _stock in the query loop, it will display the variable products at the end of the list.

Hi @jolia,

I’m not sure I understand. Can you record a video on how to replicate the issue?

Thanks,
Matej

Hi @Matej,

Please see this link. Here, iphone 15 pro is a variable product and the total stock quantity of its variables is 7 (Black 3 + Gold 0 + Green 4 = 7 ).
In the link above, I have sorted the products by _stock - descending. But as you can see, the variable product is at the end of the product list. While it should be first. Am I doing something wrong?

product loop

Hi @jolia,

when using {woo_product_stock:value}, we sum up the stock from all variable products, just like you would expect and like you figured out.

But, when you sort by _stock meta numeric key, the query checks this value. And I guess that in your case, the value is 0 or not existing, right?

I’ll mark this topic as no-bug, since it’s not really a bug, but it’s just a way how this works.
So:

  • {woo_product_stock:value} will sum up all stock values from all variations and the main product stock value.
  • _stock only looks at the main product stock value.

Hopefully, that explains it.
Matej

Thanks for the reply.

But, when you sort by _stock meta numeric key, the query checks this value. And I guess that in your case, the value is 0 or not existing, right?

Exactly, that’s right.

But one question, Is it possible to sort products by {woo_product_stock:value} in query loop?

Hi,

no, it’s not possible. As this is a calculated value and not a meta field in the product.

1 Like