Page 1 of 1

Show stock in cart

Posted: Fri Jun 07, 2013 3:28 am
by yoolabs
Hi,
I try to show the stock level of each product in the cart.
I checked the file catalog/controller/cart.php and there is a line in the product_data array:

Code: Select all

'stock'         => $result['stock'],
So I tried to change the file template/default/content/cart.tpl and copy the line for the minimum quantity, which is:

Code: Select all

<td class="l"><?php echo $product['min_qty']; ?></td>
and paste it with the following change:

Code: Select all

<td class="l2"><?php echo $product['stock']; ?></td>
But nothing is rendered. The table field remains empty.
Does anyone know a solution here?

Re: Show stock in cart

Posted: Fri Jun 07, 2013 4:27 am
by Brent
If you mean the stock level you have on hand, that is built into the cart already. You go to admin->settings to enable show stock.

Re: Show stock in cart

Posted: Fri Jun 07, 2013 5:07 am
by yoolabs
Unfortunately the stock isn´t shown in the cart.
Here are some pics of my settings, the category view, where the on hand stock is shown and the cart, without this info:
http://imageshack.us/g/707/cartrs.png/

Re: Show stock in cart

Posted: Fri Jun 07, 2013 8:59 am
by yoolabs
In the meantime I tried to compare the controller and view of the category with the controller and category of the cart because in the category the on hand stock is shown correclty.
In the category controller (catalog/ctonroller/caegory.php) is this code:

Code: Select all

'stock_level' => $result['quantity'],
and when you access this in the view of the category with:

Code: Select all

<?php echo $product['stock_level']; ?>
It shows the stock level of the item correctly.

In the cart controller ( (catalog/ctonroller/cart.php) we have:

Code: Select all

'quantity'      => $result['quantity'],
...
'stock'         => $result['stock'],
When you access these values in the cart view:

Code: Select all

<?php echo $product['quantity']; ?>
shows the selected quantity and not the in stock quantitiy.

Code: Select all

<?php echo $product['stock']; ?>

outputs nothing because it is bool, giving true, when the selected quantity is less the in stock and false if not.

I can not figure it out.

Re: Show stock in cart

Posted: Fri Jun 07, 2013 9:38 am
by Brent
Sorry, I misunderstood what you where looking at.
No, we don't have anything set up to show stock onhand in the cart.
The cart only shows the quantity being purchased, not what you have onhand.
when an item is added to the cart, it must be done from a display page like category, manufacturer, search, or one of the specialty modules, which all display the stock level onhand. It did not make sense to add it again to the cart when the customer has seen it prviously and the cart page is pretty full as it is.

Re: Show stock in cart

Posted: Mon Jun 10, 2013 12:09 am
by yoolabs
Hi,
please do not ask, if it makes sense or not, the client is king :lol:
Is there a way to implement this?
As I described above I tried it via the cart controller but I can not find out, which attribute shows the on hand stock.

Re: Show stock in cart

Posted: Mon Jun 10, 2013 3:50 am
by Brent
In cart.tpl, $product['stock'] is the onhand or stock level quantity. This is passed so we can do a check to see if there is sufficient quantity onhand to fill the order or if not, show error if that is set in admin.
You just need to make the HTML entry to display it where ever you want it.

Re: Show stock in cart

Posted: Mon Jun 10, 2013 4:37 am
by yoolabs
Hi,
please read through my answer of Friday.
I tried it with $product['stock'] but it outputs nothing.
Then I tried

Code: Select all

<?php var_dump($product['stock']);  ?>
and it outputs "boolean false" or "boolean true", depending on the the difference between selected number and on hand stock.
But it doesn´t output the number of on hand articles, like in the category.tpl.
Any other suggestions?

Re: Show stock in cart

Posted: Tue Jun 11, 2013 12:18 am
by leo
in library/cart/cart.php create a new variable e.g.
var $stock_level=0;
if you add about line 155

Code: Select all

$stock_level= (isset($product_option['quantity']) ? @$product_option['quantity'] : $product['quantity']);
var_dump($stock_level);
you will get the needed values hopefully.

Re: Show stock in cart

Posted: Tue Jun 11, 2013 8:30 am
by yoolabs
Ok, with this hint I managed to solve it with a little modification of your suggestion.
This is how it worked:

ibrary/cart/cart.php,
put this in the attribute list at around line 130:

Code: Select all

'stock_level'	=> $product['quantity'],
catalog/controller/cart.php,
put this in the attribute list at around line 180:

Code: Select all

'stock_level' => $result['stock_level'],
template/default/content/cart.tpl,
put his in the code, where the stock should be rendered:

Code: Select all

<?php echo($product['stock_level']);  ?>
Thanks a lot!