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
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.
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
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.
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.
My issue is resolved now. Thank you very much!