Reputation: 53
I have a client who is wanting to display the status of product availability in his future website. We know that you can see the status in the single product page (ie: 5 in stock) and that it's possible to display it in the store archive and category pages. However, I can't find a solution where you can display the stock status in the product widget.
Can this be achieved?
I looked in the content-widget-product.php template and tried adding:
<?php echo wp_kses_post( $availability ); ?>
But it didn't work.
Any help is much appreciated.
Thanks
Upvotes: 5
Views: 25481
Reputation: 1620
I have used this in the past to get the stock status:
get_post_meta( $product->ID, '_stock_status', true );
Upvotes: 3
Reputation: 253901
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()
Upvotes: 9