WP: Can this be modified for only Low and Zero Stock?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

WP: Can this be modified for only Low and Zero Stock?

Post by simonmlewis »

I want to alter this so if there is >0 and <3 in stock, then show

Code: Select all

echo '<div class="remaining">Only ' . number_format($product->stock,0,'','') . ' left in stock!</div>';
But if there is zero in stock, show:

Code: Select all

echo '<div class="remaining">Out of Stock</div>';
But forget about the rest. So if it's 4+, then it shows nothing.

Code: Select all

// Add Stock Quantity on Shop Page
 
function show_stock() {
global $product;
if ( $product->stock ) { // if manage stock is enabled 
if ( number_format($product->stock,0,'','') < 3 ) { // if stock is low
echo '<div class="remaining">Only ' . number_format($product->stock,0,'','') . ' left in stock!</div>';
} else {
echo '<div class="remaining">' . number_format($product->stock,0,'','') . ' left in stock</div>'; 
}
}
}
 
add_action('woocommerce_after_shop_loop_item','show_stock', 10);
I tried this but it broke it:

Code: Select all

// Add Stock Quantity on Shop Page
 
function show_stock() {
global $product;
if ( $product->stock ) { // if manage stock is enabled 
if ( number_format($product->stock,0,'','') == 0 ) { // if stock zero
echo '<div class="remaining">Out of stock</div>';
}
else
if (( number_format($product->stock,0,'','') < 3 )&& ( number_format($product->stock,0,'','') > 1 ){ // if stock is low
echo '<div class="remaining">Only ' . number_format($product->stock,0,'','') . ' left in stock!</div>';
}
}
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: WP: Can this be modified for only Low and Zero Stock?

Post by requinix »

number_format is only for display. Do not try to compare its output with actual numbers.

Code: Select all

if (amount == 0) {
	show out of stock message
} else if (amount < 3) {
	show special message
} else {
	show regular message
}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: WP: Can this be modified for only Low and Zero Stock?

Post by simonmlewis »

If I want to only show if it is OUT OF STOCK or IN STOCK, would this work?

Code: Select all

// Add Stock Quantity on Shop Page
 
function show_stock() {
global $product;
if ( $product->stock ) { // if manage stock is enabled 
if (($product->stock,0,'','') == 0 ) { // if stock zero
echo '<div class="remaining">Out of stock</div>';
}
else
{
echo '<div class="remaining">In Stock</div>';
}
}
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: WP: Can this be modified for only Low and Zero Stock?

Post by simonmlewis »

I did try this, but it broke with a 500 error.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: WP: Can this be modified for only Low and Zero Stock?

Post by requinix »

Code: Select all

if (($product->stock,0,'','') == 0 ) {
Come on. 4300+ posts. You're smarter than this.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: WP: Can this be modified for only Low and Zero Stock?

Post by simonmlewis »

Sorry? Where am I wrong with it?
The problem we have now is that for those that are >0 it is showing the In Stock message.
If it is == 0, it shows the out of stock message... but not for all those that are 0. Why might that be?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: WP: Can this be modified for only Low and Zero Stock?

Post by requinix »

The code you posted is not valid. The file it contains will not execute at all. If you now have code that is working, what is it?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: WP: Can this be modified for only Low and Zero Stock?

Post by simonmlewis »

Code: Select all

//* Add stock status to archive pages
function envy_stock_catalog() {
    global $product;
    if ( $product->is_in_stock() ) {
        echo '<div class="stock" >' . $product->get_stock_quantity() . __( ' in stock', 'envy' ) . '</div>';
    } else {
        echo '<div class="out-of-stock" >' . __( 'out of stock', 'envy' ) . '</div>';
    }
}
add_action( 'woocommerce_after_shop_loop_item_title', 'envy_stock_catalog' );
We have since found this, and for whatever reason, it shows it on ALL products correctly.
Why it works here, and not with that other code (or at least it works intermittently on the other one), I don't know.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: WP: Can this be modified for only Low and Zero Stock?

Post by requinix »

It may very well work because it's completely different from the other code.
Post Reply