Page 1 of 1

Display stock quantity only if below a certain level

Posted: Sun Mar 30, 2014 12:59 am
by raybark
Another "impulse buy" driver: I would like to have the available stock quantity be displayed, but only when its stock level reaches a low-point set for that product - "Only X Left!" for one product, while it might be "Only Y Left!" for another.

My thought being that if someone sees that there are only "so-many" left, they may be tempted to grab it quick.

Re: Display stock quantity only if below a certain level

Posted: Sun Aug 17, 2014 5:05 pm
by malcj
Hi. Just applied this solution myself. This fix will display "Only x left in stock" when the level is 5 or less. Otherwise, just says In Stock.

First, turn on stock level display on the admin panel (system/settings/edit your store/options) scroll down to Stock section and select Yes to display stock.

In your directory structure, go to catalog\controller\product. Use something like notepad to edit product.php. You to around line 282 and you should see this:

Code: Select all

if ($product_info['quantity'] <= 0) {
 $this->data['stock'] = $product_info['stock_status'];
} elseif ($this->config->get('config_stock_display')) {
 $this->data['stock'] = $product_info['quantity'];
} else {
 $this->data['stock'] = $this->language->get('text_instock');
}
replace this with:

Code: Select all

if ($product_info['quantity'] <= 0) {
 $this->data['stock'] = $product_info['stock_status'];
} elseif ($this->config->get('config_stock_display') && $product_info['quantity'] <= 5) {
 $this->data['stock'] = 'Only ' . $product_info['quantity'] . ' left in stock';
} else {
 $this->data['stock'] = $this->language->get('text_instock');
}

Re: Display stock quantity only if below a certain level

Posted: Mon Jan 19, 2015 10:03 pm
by raybark
This is a universal change only (i.e., not customizable for individual products), but for Opencart v2.0.x.x this works:

Starting at line 273 in /catalog/control/product/product.php:

Find

Code: Select all

if ($product_info['quantity'] <= 0) {
	$data['stock'] = $product_info['stock_status'];
} elseif ($this->config->get('config_stock_display')) {
	$data['stock'] = $product_info['quantity'];
} else {
	$data['stock'] = $this->language->get('text_instock');
}
Replace with

Code: Select all

if ($product_info['quantity'] <= 0) {
       $data['stock'] = $product_info['stock_status'];
} elseif ($this->config->get('config_stock_display') && $product_info['quantity'] <= 5) {
       $data['stock'] = 'Only ' . $product_info['quantity'] . ' left in stock';
} else {
        $data['stock'] = $this->language->get('text_instock');
}
Change the "5" to whatever minimum number you want.

Re: Display stock quantity only if below a certain level

Posted: Fri Jun 03, 2016 11:01 pm
by Krishna6024
How can I display the stock quantity on a search results page.

$result['quantity'] is not working.

Re: Display stock quantity only if below a certain level

Posted: Sun Dec 29, 2019 11:53 pm
by nightwing
I tested the below on OC 3.0.3.2. Only if the stock goes to 10 and below. You can change that number if you want.

Code: Select all

// Only X Left In Stock Message
			if ($product_info['quantity'] <= 0) {
			$data['stock'] = $product_info['stock_status'];
			} elseif ($this->config->get('config_stock_display') && $product_info['quantity'] <= 10) {
			$data['stock'] = 'Only ' . $product_info['quantity'] . ' left in stock';
			} else {
			$data['stock'] = $this->language->get('text_instock');
			}

Re: Display stock quantity only if below a certain level

Posted: Sat Mar 28, 2020 1:47 am
by Bjornhjacobs
Hi Sean,

I'm not a very advanced coder, could you please tell me where you added this code? Was it in product.php? Did you replace any section of the code or simply add it?
Thanks in advance!

Re: Display stock quantity only if below a certain level

Posted: Sat Mar 28, 2020 2:49 am
by straightlight
Bjornhjacobs wrote:
Sat Mar 28, 2020 1:47 am
Hi Sean,

I'm not a very advanced coder, could you please tell me where you added this code? Was it in product.php? Did you replace any section of the code or simply add it?
Thanks in advance!
Already indicated here:
Starting at line 273 in /catalog/controller/product/product.php:

Re: Display stock quantity only if below a certain level

Posted: Mon Apr 06, 2020 5:39 am
by nightwing
Hey Bjornhjacobs,

Just seeing this, I wasn't very active lately as there are a lot of things going on.
Straightlight replied above. If you are still having issues let us know.
Bjornhjacobs wrote:
Sat Mar 28, 2020 1:47 am
Hi Sean,

I'm not a very advanced coder, could you please tell me where you added this code? Was it in product.php? Did you replace any section of the code or simply add it?
Thanks in advance!