PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Oct 24, 2017 2:53 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Tue Oct 10, 2017 3:34 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4349
Location: United Kingdom
I want to alter this so if there is >0 and <3 in stock, then show
Syntax: [ Download ] [ Hide ]
echo '<div class="remaining">Only ' . number_format($product->stock,0,'','') . ' left in stock!</div>';

But if there is zero in stock, show:
Syntax: [ Download ] [ Hide ]
echo '<div class="remaining">Out of Stock</div>';

But forget about the rest. So if it's 4+, then it shows nothing.

Syntax: [ Download ] [ Hide ]
// 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:
Syntax: [ Download ] [ Hide ]
// 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.


Top
 Profile  
 
PostPosted: Tue Oct 10, 2017 4:40 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
number_format is only for display. Do not try to compare its output with actual numbers.

Code:
if (amount == 0) {
   show out of stock message
} else if (amount < 3) {
   show special message
} else {
   show regular message
}


Top
 Profile  
 
PostPosted: Tue Oct 10, 2017 5:38 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4349
Location: United Kingdom
If I want to only show if it is OUT OF STOCK or IN STOCK, would this work?

Syntax: [ Download ] [ Hide ]
// 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.


Top
 Profile  
 
PostPosted: Tue Oct 10, 2017 5:43 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4349
Location: United Kingdom
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.


Top
 Profile  
 
PostPosted: Tue Oct 10, 2017 5:55 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
Syntax: [ Download ] [ Hide ]
if (($product->stock,0,'','') == 0 ) {

Come on. 4300+ posts. You're smarter than this.


Top
 Profile  
 
PostPosted: Tue Oct 10, 2017 6:09 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4349
Location: United Kingdom
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.


Top
 Profile  
 
PostPosted: Tue Oct 10, 2017 8:42 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
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?


Top
 Profile  
 
PostPosted: Tue Oct 10, 2017 8:53 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4349
Location: United Kingdom
Syntax: [ Download ] [ Hide ]
//* 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.


Top
 Profile  
 
PostPosted: Tue Oct 10, 2017 9:32 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
It may very well work because it's completely different from the other code.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot] and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group