WooCommerce stock status in product widget
Find below the WooCommerce source code for the template content-widget-product.php
(product widget) with some customization at the end, to get stock Status:
<?php/** * ... / ... * @see https://docs.woocommerce.com/document/template-structure/ * @author WooThemes * @package WooCommerce/Templates * @version 2.5.0 */if ( ! defined( 'ABSPATH' ) ) { exit;}global $product; ?><li> <a href="<?php echo esc_url( $product->get_permalink() ); ?>"> <?php echo $product->get_image(); ?> <span class="product-title"><?php echo $product->get_name(); ?></span> </a> <?php if ( ! empty( $show_rating ) ) : ?> <?php echo wc_get_rating_html( $product->get_average_rating() ); ?> <?php endif; ?> <?php echo $product->get_price_html(); ?> <?php // Compatibility for WC versions from 2.5.x to 3.0+ if ( method_exists( $product, 'get_stock_status' ) ) { $stock_status = $product->get_stock_status(); // For version 3.0+ } else { $stock_status = $product->stock_status; // Older than version 3.0 } echo ' '.$stock_status; ?></li>
(This code is compatible from WooCommerce version 2.5.x to 3.0+)
This code is tested and works.
You can display the product Stock quantity using
WC_Product get_stock_quantity()
method.
Official documentation: WC_Product method get_stock_status()
I have used this in the past to get the stock status:
get_post_meta( $product->ID, '_stock_status', true );