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.
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.
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:
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.