Skip Navigation

[Resolved] Since views_woo_in_stock is deprecated: How can I get the information now?

This support ticket is created 2 years, 7 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: Africa/Casablanca (GMT+01:00)

This topic contains 5 replies, has 2 voices.

Last updated by fabianT 2 years, 7 months ago.

Assisted by: Jamal.

Author
Posts
#2171921

Tell us what you are trying to do?
Since technical support told me, that views_woo_in_stock is no longer updated:
https://toolset.com/forums/topic/field-views_woo_in_stock-seems-to-be-legacy-now-how-can-i-make-it-work/

I need a way to find out if a product is still in stock. Here is a example of my coding, which I have to change:

[wpv-conditional if="( $(views_woo_in_stock) eq '1' )"]

... some code to show the product, which is in stock. 

[/wpv-conditional]

What is the link to your site?
hidden link

#2172031

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting Toolset support.

Recently, I have helped one of our users to use a shortcode instead of the variable notation and it fixed the issue. Check it here https://toolset.com/forums/topic/wordpress-archive-for-products-not-updating/

If this does not work for you, allow me temporary access to your website to check the issue closely. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

I might also need to take a copy of the website for further analysis in my local development environment, let me know if that's fine with you.

#2173719

Dear Jamal,
thank you for the quick reply.

But my issue ist not the conditional-statement; this seems to work fine.

The problem is, that views_woo_in_stock is not automatically updated if the inventory is changed. Before the update, the cron job did update the variable periodically (every 5 minutes).

To put my inquiry in other words:
I need a way to get the information, if a product is still available in a conditional statement. Can you provide me with a code sample?
Either PHP or a shortcode example.

Thank you very much in advance.
Theo

#2174985

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello Theo, I searched our documentation and the changelog for our plugins and I did not find any note about deprecating this field.

Even though the cron job has been removed, I don't see why our field should not keep working. As long as the User Interface offers it as an option, it should work.

Let me check this with our 2nd Tier and get back to you.

#2175263

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

From what I could gather so far, WooCommerce will store the stock status of a product in a custom field (_stock_status). The stock status is saved as a string. The default values are: "instock", "outofstock", and "onbackorder". This means that you can check the field directly:

[wpv-conditional if="( '[wpv-post-field name="_stock_status"]' eq 'instock' )"]
 ... We have a stock for this product ... 
[/wpv-conditional]

The types shortcode also works as expected. It will return 1 for stock availability and 0 if no stock is available:

[wpv-conditional if="( '[types field='views_woo_in_stock'][/types]' eq '1' )"]In stock[/wpv-conditional]
[wpv-conditional if="( '[types field='views_woo_in_stock'][/types]' eq '0' )"]Out of stock[/wpv-conditional]

I just run a test and both work as expected. Once a product is not available(after selling the available stock), both of these shortcodes will return the new value. No need for any cron jobs, or custom code for it.

If you want to build your own shortcode, you can rely on the get_stock_status() function of the WC_Product class:

$product = wc_get_product( $product_id );
// OR
global $product; // for the current product

return $product->get_stock_status();

This will return the value of the _stock_status field: "instock", "outofstock", or "onbackorder"

I hope this answers your question. Let me know if you have any further questions.

#2176001

My issue is resolved now. Thank you very much!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.