I'm using this piece of code in my functions.php
file of my WordPress/WooCommerce site:
function envy_stock_catalog() {
global $product;
if ( $product->is_in_stock() ) {
echo $product->get_stock_quantity() ;
} else {
echo '<div class="out-of-stock" >' . __( 'out of stock', 'envy' ) . '</div>';
add_action('init','remove_loop_button');
}
}
add_action( 'woocommerce_after_shop_loop_item_title', 'envy_stock_catalog' );
This code displays the 'out of stock' notice on the shop page where all products are displayed like this:
The issue is that it also causes of the available inventory number of the products next to product titles like this:
I don't want this.
My question:
How I can change my code to still display the 'out of stock'
notice (when product is out of stock) and not the inventory product availability on shop page (in left side of add-to-cart button)?
Thanks in advance!
Explanations about your code:
With the code below, now we have:
!is_shop()
which displays stock quantity as long as product is is stock and is not in shop page.So here is the fully functional solution, as desired:
If you just want to display stock quantity on single product page, and 'out of stock' only in shop page, this will be your code:
Now if you just don't want to display any stock quantity, your code will be like this:
And in this case you could use all the working tricks from this answer:
Woocommerce - Remove the available product inventory number from the shop page
All code is tested and works perfectly.
The code goes on
function.php
file of your active child theme or theme…