Post by vsilly » Fri May 11, 2012 11:57 pm

Hello folks, brand-new open cart user. Reviewing admin settings it seems as though empty zero inventory products and product categories will still show in opencart unless I manually disable each one, is this correct?

Is there a way to make categories and products with zero inventory automatically disable/hide?

Is this something that is available in the interface or an extension?
Thank you very much

Newbie

Posts

Joined
Wed May 09, 2012 10:59 am

Post by Avvici » Sat May 12, 2012 12:57 pm

A couple steps will get you there. v1.5.2.1
Open catalog/controller/product/category.php and find this line:

Code: Select all

$results = $this->model_catalog_product->getProducts($data);
			
			foreach ($results as $result) {
Right below it add this code:

Code: Select all

$product_info = $this->model_catalog_product->getProductQuantity($result['product_id']);
               //simple boolean return based on condition
               if ($product_info['quantity'] <= 0) {
                  $stock = false;
               } else {
                   $stock = true;
               }
Now find this line:

Code: Select all

$this->data['products'][] = array(
Directly below it add this to the array:

Code: Select all

'stock'  => $stock,
Now we create our new function to check the quantity. We don't want to use getProduct because its way to heavy of a query just to find the quantity only. Open: catalog/model/catalog/product.php and find this line:

Code: Select all

class ModelCatalogProduct extends Model {
Directly after it add this:

Code: Select all

public function getProductQuantity($product_id) {
		
		$query = $this->db->query("SELECT quantity FROM " . DB_PREFIX . "product  WHERE product_id = '" . (int)$product_id . "'");
		if ($query->num_rows) {
			
			return $query->row;
		} else {
			return false;
		}
	}
Now open catalog/view/theme/default/template/product/category.tpl and find this entire block of code:

Code: Select all

 <div class="product-list">
    <?php foreach ($products as $product) { ?>
    <div>
      <?php if ($product['thumb']) { ?>
      <div class="image"><a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" title="<?php echo $product['name']; ?>" alt="<?php echo $product['name']; ?>" /></a></div>
      <?php } ?>
      <div class="name"><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></div>
      <div class="description"><?php echo $product['description']; ?></div>
      <?php if ($product['price']) { ?>
      <div class="price">
        <?php if (!$product['special']) { ?>
        <?php echo $product['price']; ?>
        <?php } else { ?>
        <span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span>
        <?php } ?>
        <?php if ($product['tax']) { ?>
        <br />
        <span class="price-tax"><?php echo $text_tax; ?> <?php echo $product['tax']; ?></span>
        <?php } ?>
      </div>
      <?php } ?>
      <?php if ($product['rating']) { ?>
      <div class="rating"><img src="catalog/view/theme/default/image/stars-<?php echo $product['rating']; ?>.png" alt="<?php echo $product['reviews']; ?>" /></div>
      <?php } ?>
      <div class="cart">
        <input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button" />
      </div>
      <div class="wishlist"><a onclick="addToWishList('<?php echo $product['product_id']; ?>');"><?php echo $button_wishlist; ?></a></div>
      <div class="compare"><a onclick="addToCompare('<?php echo $product['product_id']; ?>');"><?php echo $button_compare; ?></a></div>
    </div>
    <?php } ?>
  </div>
Make it look like this (adding the stock check at the beginning <?php if($stock){?>). All products that returned FALSE ( 0 stock) will not show.

Code: Select all

    <?php foreach ($products as $product) { ?>
    <?php if($product['stock']){?>
    <div>
      <?php if ($product['thumb']) { ?>
      <div class="image"><a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" title="<?php echo $product['name']; ?>" alt="<?php echo $product['name']; ?>" /></a></div>
      <?php } ?>
      <div class="name"><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></div>
      <div class="description"><?php echo $product['description']; ?></div>
      <?php if ($product['price']) { ?>
      <div class="price">
        <?php if (!$product['special']) { ?>
        <?php echo $product['price']; ?>
        <?php } else { ?>
        <span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span>
        <?php } ?>
        <?php if ($product['tax']) { ?>
        <br />
        <span class="price-tax"><?php echo $text_tax; ?> <?php echo $product['tax']; ?></span>
        <?php } ?>
      </div>
      <?php } ?>
      <?php if ($product['rating']) { ?>
      <div class="rating"><img src="catalog/view/theme/default/image/stars-<?php echo $product['rating']; ?>.png" alt="<?php echo $product['reviews']; ?>" /></div>
      <?php } ?>
      <div class="cart">
        <input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button" />
      </div>
      <div class="wishlist"><a onclick="addToWishList('<?php echo $product['product_id']; ?>');"><?php echo $button_wishlist; ?></a></div>
      <div class="compare"><a onclick="addToCompare('<?php echo $product['product_id']; ?>');"><?php echo $button_compare; ?></a></div>
    </div>
      <?php } ?>
    <?php } ?>
  </div>
It's in my hopes that Jay, Q, Jonathan, or i2paq won't come back to this and offer an extension that does just this lol.
Last edited by Avvici on Sat May 12, 2012 1:04 pm, edited 1 time in total.

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by florinsith » Sat May 12, 2012 1:03 pm

Yes, that is corect, thats why there are the Out of stock Statuses, to inform the customer that the product is out of stock and also let him know about future purchase possibilities.
If you still dont want them, edit catalog/model/catalog/product.php. In the mentioned file, you will find 3 times the piece of code:

Code: Select all

pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1'
and each time replace it with:

Code: Select all

pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.quantity > '0'
That takes care of products. Now (hiding categories) edit the file catalog/controller/module/category.php and find the code:

Code: Select all

if ($setting['count']) {
					$product_total = $this->model_catalog_product->getTotalProducts($data);
					
					$children_data[] = array(
						'category_id' => $child['category_id'],
						'name'        => $child['name'] . ' (' . $product_total . ')',
						'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])	
					);						
				} else {
					$children_data[] = array(
						'category_id' => $child['category_id'],
						'name'        => $child['name'],
						'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])	
					);						
				}
replace it with:

Code: Select all

$product_total = $this->model_catalog_product->getTotalProducts($data);
				if ($product_total > 0) {	
				if ($setting['count']) {
					
					$children_data[] = array(
						'category_id' => $child['category_id'],
						'name'        => $child['name'] . ' (' . $product_total . ')',
						'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])	
					);						
				} else {
					$children_data[] = array(
						'category_id' => $child['category_id'],
						'name'        => $child['name'],
						'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])	
					);						
				}
                }
Next find the code:

Code: Select all

if ($setting['count']) {
				$product_total = $this->model_catalog_product->getTotalProducts($data);
			
				$this->data['categories'][] = array(
					'category_id' => $category['category_id'],
					'name'        => $category['name'] . ' (' . $product_total . ')',
					'children'    => $children_data,
					'href'        => $this->url->link('product/category', 'path=' . $category['category_id'])
				);				
			} else {
				$this->data['categories'][] = array(
					'category_id' => $category['category_id'],
					'name'        => $category['name'],
					'children'    => $children_data,
					'href'        => $this->url->link('product/category', 'path=' . $category['category_id'])
				);			
			}
replace it with:

Code: Select all

$product_total = $this->model_catalog_product->getTotalProducts($data);
			if ($product_total > 0) {
			if ($setting['count']) {
			
				$this->data['categories'][] = array(
					'category_id' => $category['category_id'],
					'name'        => $category['name'] . ' (' . $product_total . ')',
					'children'    => $children_data,
					'href'        => $this->url->link('product/category', 'path=' . $category['category_id'])
				);				
			} else {
				$this->data['categories'][] = array(
					'category_id' => $category['category_id'],
					'name'        => $category['name'],
					'children'    => $children_data,
					'href'        => $this->url->link('product/category', 'path=' . $category['category_id'])
				);			
			}
			}
Nex edit the file catalog/controller/product/category.php and find the code:

Code: Select all

$this->data['categories'][] = array(
					'name'  => $result['name'] . ' (' . $product_total . ')',
					'href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url)
				);

and replace it with:

Code: Select all

				if ($product_total > 0) {
				$this->data['categories'][] = array(
					'name'  => $result['name'] . ' (' . $product_total . ')',
					'href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url)
				);
				}

Image
My Extensions - SuperTheme - Opencart templates - Opencart modules


User avatar
Expert Member

Posts

Joined
Fri May 14, 2010 2:36 am


Post by Avvici » Sat May 12, 2012 1:06 pm

Just to clarify, I set you up to not show products that are out of stock on the CATEGORY LISTING PAGES. The other solution is something completely different. You got more bang for your buck on this one lol.

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by samolesong76 » Sat May 12, 2012 1:10 pm

avvici wrote:A couple steps will get you there. v1.5.2.1
Open catalog/controller/product/category.php and find this line:

Code: Select all

$results = $this->model_catalog_product->getProducts($data);
			
			foreach ($results as $result) {
Right below it add this code:

Code: Select all

$product_info = $this->model_catalog_product->getProductQuantity($result['product_id']);
               //simple boolean return based on condition
               if ($product_info['quantity'] <= 0) {
                  $stock = false;
               } else {
                   $stock = true;
               }
Now find this line:

Code: Select all

$this->data['products'][] = array(
Directly below it add this to the array:

Code: Select all

'stock'  => $stock,
Now we create our new function to check the quantity. We don't want to use getProduct because its way to heavy of a query just to find the quantity only. Open: catalog/model/catalog/product.php and find this line:

Code: Select all

class ModelCatalogProduct extends Model {
Directly after it add this:

Code: Select all

public function getProductQuantity($product_id) {
		
		$query = $this->db->query("SELECT quantity FROM " . DB_PREFIX . "product  WHERE product_id = '" . (int)$product_id . "'");
		if ($query->num_rows) {
			
			return $query->row;
		} else {
			return false;
		}
	}
Now open catalog/view/theme/default/template/product/category.tpl and find this entire block of code:

Code: Select all

 <div class="product-list">
    <?php foreach ($products as $product) { ?>
    <div>
      <?php if ($product['thumb']) { ?>
      <div class="image"><a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" title="<?php echo $product['name']; ?>" alt="<?php echo $product['name']; ?>" /></a></div>
      <?php } ?>
      <div class="name"><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></div>
      <div class="description"><?php echo $product['description']; ?></div>
      <?php if ($product['price']) { ?>
      <div class="price">
        <?php if (!$product['special']) { ?>
        <?php echo $product['price']; ?>
        <?php } else { ?>
        <span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span>
        <?php } ?>
        <?php if ($product['tax']) { ?>
        <br />
        <span class="price-tax"><?php echo $text_tax; ?> <?php echo $product['tax']; ?></span>
        <?php } ?>
      </div>
      <?php } ?>
      <?php if ($product['rating']) { ?>
      <div class="rating"><img src="catalog/view/theme/default/image/stars-<?php echo $product['rating']; ?>.png" alt="<?php echo $product['reviews']; ?>" /></div>
      <?php } ?>
      <div class="cart">
        <input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button" />
      </div>
      <div class="wishlist"><a onclick="addToWishList('<?php echo $product['product_id']; ?>');"><?php echo $button_wishlist; ?></a></div>
      <div class="compare"><a onclick="addToCompare('<?php echo $product['product_id']; ?>');"><?php echo $button_compare; ?></a></div>
    </div>
    <?php } ?>
  </div>
Make it look like this (adding the stock check at the beginning <?php if($stock){?>). All products that returned FALSE ( 0 stock) will not show.

Code: Select all

    <?php foreach ($products as $product) { ?>
    <?php if($product['stock']){?>
    <div>
      <?php if ($product['thumb']) { ?>
      <div class="image"><a href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" title="<?php echo $product['name']; ?>" alt="<?php echo $product['name']; ?>" /></a></div>
      <?php } ?>
      <div class="name"><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></div>
      <div class="description"><?php echo $product['description']; ?></div>
      <?php if ($product['price']) { ?>
      <div class="price">
        <?php if (!$product['special']) { ?>
        <?php echo $product['price']; ?>
        <?php } else { ?>
        <span class="price-old"><?php echo $product['price']; ?></span> <span class="price-new"><?php echo $product['special']; ?></span>
        <?php } ?>
        <?php if ($product['tax']) { ?>
        <br />
        <span class="price-tax"><?php echo $text_tax; ?> <?php echo $product['tax']; ?></span>
        <?php } ?>
      </div>
      <?php } ?>
      <?php if ($product['rating']) { ?>
      <div class="rating"><img src="catalog/view/theme/default/image/stars-<?php echo $product['rating']; ?>.png" alt="<?php echo $product['reviews']; ?>" /></div>
      <?php } ?>
      <div class="cart">
        <input type="button" value="<?php echo $button_cart; ?>" onclick="addToCart('<?php echo $product['product_id']; ?>');" class="button" />
      </div>
      <div class="wishlist"><a onclick="addToWishList('<?php echo $product['product_id']; ?>');"><?php echo $button_wishlist; ?></a></div>
      <div class="compare"><a onclick="addToCompare('<?php echo $product['product_id']; ?>');"><?php echo $button_compare; ?></a></div>
    </div>
      <?php } ?>
    <?php } ?>
  </div>
It's in my hopes that Jay, Q, Jonathan, or i2paq won't come back to this and offer an extension that does just this lol.
Hey thanks, I am actually glad there are different solutions here because I was looking for just this. I however just wanted to show a message that said OUT OF STOCK so I just did a <?php if(!$product['stock']){ to check for FALSE and works fine.

New member

Posts

Joined
Sun Apr 22, 2012 12:39 am

Post by florinsith » Sat May 12, 2012 1:13 pm

Sorry, was writing my post and didnt know you were allready answearing. But since multiple solutions are welcome, Ill leave it.

Image
My Extensions - SuperTheme - Opencart templates - Opencart modules


User avatar
Expert Member

Posts

Joined
Fri May 14, 2010 2:36 am


Post by Avvici » Sat May 12, 2012 1:17 pm

Na, I am sure your solution is geared towards his question. It's all good.

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by vsilly » Sat May 12, 2012 1:44 pm

Wow, thanks for the detailed answer.
Was kind of hoping for an extension...
;-)

Newbie

Posts

Joined
Wed May 09, 2012 10:59 am

Post by onurokumus » Wed Oct 10, 2012 6:51 pm

florinsith: I've done what you given. It worked yesterday but without touching any codes it doesn't work today. How come? ???

Newbie

Posts

Joined
Wed Oct 10, 2012 6:49 pm

Post by florinsith » Sat Oct 20, 2012 8:08 pm

Wll if yesterday works and now doesnt perhaps there was a problem on the server and the hoster reinstated an older backup that didnt have the modifications. Verify that.

Image
My Extensions - SuperTheme - Opencart templates - Opencart modules


User avatar
Expert Member

Posts

Joined
Fri May 14, 2010 2:36 am


Post by digitiba » Thu Jan 10, 2013 2:12 am

Hello gentlemen, I am working with version 1.5.4.1. of OpenCart, and observing the code a bit, I realized that this version has some significant changes, make it impossible to change this way presented. You could try to help me achieve the second option, where it is reported that the product is unavailable in the version I use the 1.5.4.1. Thanks to all and sorry for my poor english ...

Newbie

Posts

Joined
Thu Jan 10, 2013 2:08 am

Post by ainosilva » Tue Feb 12, 2013 4:47 am

HELLO!

I'm so happy I could just found the solution for this! Don't know if it's perfect so do a backup before, and LET ME KNOW if something is wrong...
Version OPEN CART: v1.5.5.1, you can dowload here: http://www.opencart.com/index.php?route ... n_id=10507

or just follow the instructions:

you open two pages: /catalog/model/catalog/product.php
and: /catalog/controller/product/category.php

Now in product.php you search for:

Code: Select all

p.date_available <= NOW()
and replace with

Code: Select all

p.date_available <= NOW()   and p.quantity>=1
In category.php you search for:

Code: Select all

$product_total = $this->model_catalog_product->getTotalProducts($data); 
pay attention here, because there are two, it's the second one! And replace with

Code: Select all

//$product_total = $this->model_catalog_product->getTotalProducts($data); 
			$product_total=0;

and after this line

Code: Select all

foreach ($results as $result) {
add this code

Code: Select all

if ($result['quantity']>=1) {
					$product_total=$product_total+1;
and of course just close with a } before the line:

Code: Select all

$url = '';
Hope it works for you, like it did for me! :)

Newbie

Posts

Joined
Tue Feb 12, 2013 4:22 am

Post by johnZer » Tue Feb 19, 2013 4:25 am

Hi,
I just tryed this code, but the categories that have no product in stock are displayed in the horizontal menu of navigation and the vertical menu/strip (when you're in a product page)
Is it possible to hide such categories as well ? (fr OpenCart V 1.5.4)
thank you for your help !!

Newbie

Posts

Joined
Mon Feb 18, 2013 12:30 pm

Post by ainosilva » Wed Mar 13, 2013 12:53 am

It's hard to hide that categories. If you add a new one with no products, it will appear anyway..

Newbie

Posts

Joined
Tue Feb 12, 2013 4:22 am

Post by billkou » Sat Sep 14, 2013 10:59 pm

Hello people.

Just a few changes for Opencart 1.5.6 in order to hide out of stock products. The solution given might work on previous versions but on my 1.5.6 version it gave http 500 errors or didn't work at all and then I thought seeing the code and to check for myself if I there's something I could do. ( I'm not a programmer but the changes actually are two lines of code )

The changes on the file : /catalog/model/catalog/product.php are the same as posted above.

Now in product.php you search for:

Code: Select all

p.date_available <= NOW()
and replace with

Code: Select all

p.date_available <= NOW()   and p.quantity>=1
Now, the changes for Opencart 1.5.6 ( I don't know any other version of Opencart as I'm new to this and only worked with 1.5.6 ) are in the file /catalog/controller/product/category.php

Code: Select all

$product_total = $this->model_catalog_product->getTotalProducts($data);


on category.php is found on two lines. Around 186 and around 206. The one that needs to be changed is the one on line 186.

Code: Select all

foreach ($results as $result) { 


also is found on two lines. 178 and 211. We need to put

Code: Select all

if ($result['quantity']>=1) {
               $product_total=$product_total+1;
               }
after the one that is in line 211.

New member

Posts

Joined
Mon Sep 02, 2013 6:41 am

Post by ainosilva » Mon Sep 16, 2013 5:34 pm

thank you billkou! I'm sorry I didn't thought the new version was that different! :)

Newbie

Posts

Joined
Tue Feb 12, 2013 4:22 am

Post by garywinner » Fri Jan 24, 2014 12:45 am

I follow the latest post to do the change.
The out of stock product is hidden. But the category with ZERO count still appear.
And all the category count turn to zero.

How can I also hide the category with zero count.
And makes the category with non-zero count stay.

Many thanks!

New member

Posts

Joined
Sun Sep 01, 2013 6:31 pm
Location - Hong Kong

Post by xlam » Sun Mar 16, 2014 2:56 am

ainosilva wrote:HELLO!

I'm so happy I could just found the solution for this! Don't know if it's perfect so do a backup before, and LET ME KNOW if something is wrong...
Version OPEN CART: v1.5.5.1, you can dowload here: http://www.opencart.com/index.php?route ... n_id=10507

or just follow the instructions:

you open two pages: /catalog/model/catalog/product.php
and: /catalog/controller/product/category.php

Now in product.php you search for:

Code: Select all

p.date_available <= NOW()
and replace with

Code: Select all

p.date_available <= NOW()   and p.quantity>=1
In category.php you search for:

Code: Select all

$product_total = $this->model_catalog_product->getTotalProducts($data); 
pay attention here, because there are two, it's the second one! And replace with

Code: Select all

//$product_total = $this->model_catalog_product->getTotalProducts($data); 
			$product_total=0;

and after this line

Code: Select all

foreach ($results as $result) {
add this code

Code: Select all

if ($result['quantity']>=1) {
					$product_total=$product_total+1;
and of course just close with a } before the line:

Code: Select all

$url = '';
Hope it works for you, like it did for me! :)
working for me i'm use 1.5.5.1.

Baju Muslim Baju Pria Baju Anak Baju Korea Sparepart Printer Baju Muslim JNE Surabaya


New member

Posts

Joined
Sun Dec 25, 2011 2:58 pm

Post by xlam » Tue Apr 08, 2014 11:37 am

Can any body help me please?

I use this trick and working fine. But if i want set one selected category to show all product, include available & out of stock product how do that?

Thanks before

Baju Muslim Baju Pria Baju Anak Baju Korea Sparepart Printer Baju Muslim JNE Surabaya


New member

Posts

Joined
Sun Dec 25, 2011 2:58 pm

Post by lowpitch » Wed Apr 30, 2014 3:48 pm

In response to the solutions by ainosilva and billkou, I don't think any of the changes to the controller files are necessary. When the changes to the model file are done then the out of stock products will automatically not be included in $product_total in the controller file.

Newbie

Posts

Joined
Sat Apr 26, 2014 8:44 pm
Who is online

Users browsing this forum: No registered users and 15 guests