Skip Navigation

[Resolved] Display an out of stock badge only if all variations are out of stock

This thread is resolved. Here is a description of the problem and solution.

Problem:

I am finding that when a product is variable and one of the variations is out of stock, it sets the "views_woo_in_stock to false/0 even if another variation still has stock. I would like to only show and Out of Stock badge if ALL of the variations are out of stock.

Solution:

Please try these:
Dashboard-> Toolset-> WooCommerce Views
in section "Products Fields for Parametric Searches", click button "Calculate Now".

It will update all field "views_woo_in_stock" values of product posts.

Relevant Documentation:

This support ticket is created 4 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 3 voices.

Last updated by ellenB 4 years, 10 months ago.

Assisted by: Luo Yang.

Author
Posts
#1434963

I am using Toolset Views with WooCommerce and am adding custom badges in the Views. For the Out of Stock view, I have this code:

<div class="badges">
[wpv-conditional if="( $(views_woo_on_sale) eq '1' ) AND ( $(views_woo_in_stock) ne '0' )"]
<img src='/wp-content/uploads/2018/02/sale-tag-left.png'>
[/wpv-conditional]
[wpv-conditional if="( $(views_woo_in_stock) eq '0' )"]
<img src='/wp-content/uploads/2020/01/out-of-stock-badge-left.png'>
[/wpv-conditional]
</div>

I am finding that when a product is variable and one of the variations is out of stock, it sets the "views_woo_in_stock to false/0 even if another variation still has stock. I would like to only show and Out of Stock badge if ALL of the variations are out of stock.

The only ticket I found that is close to my issue is this one: https://toolset.com/forums/topic/cannot-get-correct-stock-status-for-woocommerce-variable-products/ but the goal of the ticket creator is not the same as mine. That is where I learned that a product will show out of stock if only one variation is out of stock.

How do I only show the badge if all variations are out of stock?

My development site is at hidden link. It is password protected.

#1435515

Hello,

I assume you hare installed Toolset WooCommerce Views in your website:
https://toolset.com/download/toolset-woocommerce-views/

I have tried it in my localhost with a fresh WordPress insallation + Woocommerce plugin + the latest version of Toolset plugins.
The shortcodes you mentioned above works fine:

[wpv-conditional if="( $(views_woo_on_sale) eq '1' ) AND ( $(views_woo_in_stock) ne '0' )"]
<img src='/wp-content/uploads/2018/02/sale-tag-left.png'>
[/wpv-conditional]
[wpv-conditional if="( $(views_woo_in_stock) eq '0' )"]
<img src='/wp-content/uploads/2020/01/out-of-stock-badge-left.png'>
[/wpv-conditional]

But there is one thing need to pay attention, the custom field "views_woo_on_sale" is for "on sale" products, so if you don't need it, you can remove it, for example:

[wpv-conditional if="( $(views_woo_in_stock) ne '0' )"]
<img src='/wp-content/uploads/2018/02/sale-tag-left.png'>
[/wpv-conditional]
[wpv-conditional if="( $(views_woo_in_stock) eq '0' )"]
<img src='/wp-content/uploads/2020/01/out-of-stock-badge-left.png'>
[/wpv-conditional]
#1436473
animal-talk-hardback.jpg
animal-talk-board-book.jpg
animal-talk-out-of-stock.JPG

I do want to display the on sale badge and that code is working fine.

Regarding the out of stock badge, the code works BUT it displays an out of stock badge if ONLY ONE of the variation is out of stock.

For example:

The book Animal Talk shows the Out of Stock badge. See image: animal-talk-out-of-stock.jpg

The book is out of stock for the Board Book variation. See image animal-talk-board-book.jpg

However, the book IS NOT out of stock for the Hardback variation. See image animal-talk-hardback.jpg

So the result is that customers will think that the Animal Talk book is out of stock because there is an Out of Stock badge on the products page even though only the board book version is out of stock and they can still purchase the Hardback variation/version.

I want the Out of Stock badge to ONLY show up if ALL of the variations are out of stock. Does that make sense?

#1437973

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Luo is on holiday today. He will get in touch with you as soon as he will get back to work.

#1439055

Thanks for the details, please try these:
Dashboard-> Toolset-> WooCommerce Views
in section "Products Fields for Parametric Searches", click button "Calculate Now".

It will update all field "views_woo_in_stock" values of product posts.

Then test it in front-end, you should be able to see those shortcodes you mentioned above work fine.

And you can also switch to option "Using the WordPress cron", then it will update the field "views_woo_in_stock" value automatically.

See our document:
https://toolset.com/documentation/user-guides/views/woocommerce-views-calculated-fields-and-batch-update/#wcv-update-calculated
section "Manual and batch update for WooCommerce calculated fields"

#1444671

That did the trick. Thank you!

#1444673

My issue is resolved now. Thank you!