Page 1 of 2

[Solved] Out of Stock or Add to Cart button on categories Pg

Posted: Fri Sep 23, 2011 10:30 pm
by 4bco
I would like to modify the categories page to either display, "Out of Stock" or and "Add to Cart" button...

Any help is greatly appreciated.

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 3:24 am
by 4bco
I know I need to reference the stock quantity in the controller file for the catalog and then modify the catalog template file for the catalog with something like....

Code: Select all

			<?php if ($stock <= 0) {
				echo (<b>Out Of Stock</b>)
			} else { 
				<a class="button" href="<?php echo $products[$j]['add']; ?>" title="<?php echo $button_add_to_cart; ?>" ><span><?php echo $button_add_to_cart; ?></span></a>
			} ?>
Again, any assistance is appreciated.

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 4:59 am
by uksitebuilder
This is my take on it:

edit: catalog/model/catalog/product.php

before the closing php tag add

Code: Select all

	public function getProductHasStock($product_id) {
		$query = $this->db->query("SELECT quantity FROMM " . DB_PREFIX . "product WHERE product_id = '" . (int)$product_id . "'");
		
		if($query->row['total']>0){
			return true;
		}else{
			return false;
		}
	}	
edit: catalog/controller/product/category.php

find

Code: Select all

$this->data['products'][] = array(
add after

Code: Select all

					'stock'		  => ($this->model_catalog_product->getProductHasStock($result['product_id'])?1:0),

Your template code should work with this too.

I haven't checked this, so it could be an epic fail.

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 5:14 am
by 4bco
Thanks Simon ~

However, if I read your code correctly, the items will only be returned if they are in stock...What I need is for the item to be returned either way, but if the available quantity is zero, it needs to say "Out of Stock" and if it is available, the add to cart button should be visible.

When the items will be in season, it will be changed to "Pre-Order" and so the custom should be able to see all of the items whether they are available for immediate purchase or not.

Let me know if you can help me get this. Thanks!

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 5:31 am
by uksitebuilder
Hi, I think you slightly misread my code.

It will return $product['stock'] as either 1 or 0 (or at least it should)

Then your code you put in the template will do what you expect if you change $stock to $product['stock']

Try it and see :)

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 5:34 am
by uksitebuilder
My code is based on 1.5.x

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 6:20 am
by 4bco
I have 1.4.9.4....but I am working on this now with the code you gave me.

On the template's category.tlp file I now have...

Code: Select all

         <?php if ($product['stock'] = 0) {
            echo '<b>Out Of Stock</b>';
         } else { } ?>
            <a class="button" href="<?php echo $products[$j]['add']; ?>" title="<?php echo $button_add_to_cart; ?>" ><span><?php echo $button_add_to_cart; ?></span></a>
On the Model product.php file I now have....changed, total to quantity (not sure if this is right, but total did not work)

Code: Select all

	   public function getProductHasStock($product_id) {
      $query = $this->db->query("SELECT quantity FROM " . DB_PREFIX . "product WHERE product_id = '" . (int)$product_id . "'");
      
      if($query->row['quantity']>0){
         return true;
      }else{
         return false;
      }
   } 
On the controller category.php file I have

Code: Select all

'stock'   => ($this->model_catalog_product->getProductHasStock($result['product_id'])?1:0),
This code does not return any errors, but it also does not place the "out of stock" info with the item.

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 6:21 am
by 4bco
The other issue I have is that I want the button to be part of the else in the code, but I can't seem to get it right with the mixture of php and html.....Please help!

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 6:23 am
by uksitebuilder
couple of errors in your tpl code

change

if ($product['stock'] = 0

to

if ($product['stock'] == 0

and change

} else { }

to

} else {

and pop a <?php } ?> after your button html

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 6:27 am
by uksitebuilder
Here you go

Code: Select all

<?php if ($product['stock'] == 0) {
            echo '<b>Out Of Stock</b>';
         } else { ?>
            <a class="button" href="<?php echo $products[$j]['add']; ?>" title="<?php echo $button_add_to_cart; ?>" ><span><?php echo $button_add_to_cart; ?></span></a><?php } ?>

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 6:32 am
by 4bco
hmmm...

Now I get

Notice: Undefined variable: product in /catalog/view/theme/dillard/template/product/category.tpl on line 63
Out Of Stock

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 6:37 am
by 4bco
This got it....

Code: Select all

<?php if ($products[$j]['stock'] == 0) {
            echo '<b>Out Of Stock</b>';
         } else { ?>
            <a class="button" href="<?php echo $products[$j]['add']; ?>" title="<?php echo $button_add_to_cart; ?>" ><span><?php echo $button_add_to_cart; ?></span></a><?php } ?>  

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 6:37 am
by 4bco
Thank you for all your help....Do you really have 14 kids?

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 6:38 am
by uksitebuilder
I have just downloaded 1.4.9.4 - I can see slight differences between 1.5. and 1.4.x code
One moment and I will recheck my code and yours and come back with a solution.

Haha @ 14 kids. What do you think I am, a robot? lol

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 6:42 am
by uksitebuilder
oh you fixed it ? great stuff :)

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 6:45 am
by 4bco
I asked about the 14 kids b/c that is what you had in your signature....lol

I myself have only 6, but with 6 kids, I could only make a small donation to say thank you for your help!

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 6:49 am
by uksitebuilder
Much appreciated.

My sig is a bit of a joke, or is meant to be hehe.

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 6:52 am
by 4bco
I figured so...lol...that's why I asked :o :laugh:

Re: Out of Stock or Add to Cart button on categories Page

Posted: Sat Sep 24, 2011 6:53 am
by uksitebuilder
They've multiplied now ;)

Re: [Solved] Out of Stock or Add to Cart button on categorie

Posted: Mon Nov 05, 2012 8:18 am
by SimonHavelock
Hi!
I'm trying to implement this code but after everything is done what I get is:

Code: Select all

Notice: Undefined index: total in /home/simon/www/simon.wine-express.pl/htdocs/catalog/model/catalog/product.php on line 516Notice: Undefined index: total in /home/simon/www/simon.wine-express.pl/htdocs/catalog/model/catalog/product.php on line 516N
A couple of times, on category page.

What should I change?