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
Open catalog/controller/product/category.php and find this line:
Code: Select all
$results = $this->model_catalog_product->getProducts($data);
foreach ($results as $result) {
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;
}
Code: Select all
$this->data['products'][] = array(
Code: Select all
'stock' => $stock,
Code: Select all
class ModelCatalogProduct extends Model {
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;
}
}
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>
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>
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'
Code: Select all
pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.quantity > '0'
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'])
);
}
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'])
);
}
}
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'])
);
}
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'])
);
}
}
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)
);
}
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.avvici wrote:A couple steps will get you there. v1.5.2.1
Open catalog/controller/product/category.php and find this line:Right below it add this code:Code: Select all
$results = $this->model_catalog_product->getProducts($data); foreach ($results as $result) {
Now find this line: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; }
Directly below it add this to the array:Code: Select all
$this->data['products'][] = array(
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
'stock' => $stock,
Directly after it add this:Code: Select all
class ModelCatalogProduct extends Model {
Now open catalog/view/theme/default/template/product/category.tpl and find this entire block of code: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; } }
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
<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>
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.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>
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()
Code: Select all
p.date_available <= NOW() and p.quantity>=1
Code: Select all
$product_total = $this->model_catalog_product->getTotalProducts($data);
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) {
Code: Select all
if ($result['quantity']>=1) {
$product_total=$product_total+1;
Code: Select all
$url = '';
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 !!
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()
Code: Select all
p.date_available <= NOW() and p.quantity>=1
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;
}
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!
working for me i'm use 1.5.5.1.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:and replace withCode: Select all
p.date_available <= NOW()
In category.php you search for:Code: Select all
p.date_available <= NOW() and p.quantity>=1
pay attention here, because there are two, it's the second one! And replace withCode: Select all
$product_total = $this->model_catalog_product->getTotalProducts($data);
Code: Select all
//$product_total = $this->model_catalog_product->getTotalProducts($data); $product_total=0;
and after this lineadd this codeCode: Select all
foreach ($results as $result) {
and of course just close with a } before the line:Code: Select all
if ($result['quantity']>=1) { $product_total=$product_total+1;
Hope it works for you, like it did for me!Code: Select all
$url = '';
Baju Muslim Baju Pria Baju Anak Baju Korea Sparepart Printer Baju Muslim JNE Surabaya
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
Users browsing this forum: No registered users and 14 guests