Show quantity in cart on each product_info.php page

By | July 21, 2009

On the official osCommerce forum, Will asks;

Does anyone know how I could had on the product page (product_info.php), near the buy it now button, a line of text saying: You have XXXX units of this item currently in your cart.

I didn’t really read the question so gave the advice to use the in_cart class for this. Wrong!

Will then went away and came up with some code to do this, which is like this:

[php]$superid = $product_info[‘products_id’];
$products = $cart->get_products();
for ($i=0, $n=sizeof($products); $i<$n; $i++) { if ($products[$i]['id'] == $superid) { $qtyencart = $products[$i]['quantity']; echo"Vous avez $qtyencart unité de cet item dans votre panier actuellement.”;
break;
}
}[/php]

Workable, but ugly and complicated. Here’s my take on getting the same thing, coded to osCommerce conventions;

[php]echo $cart->get_quantity($product_info[‘products_id’]);[/php]

will bring you the number of products in the cart on the product info page. Use this with sprintf to show the text;

includes/languages/english/product_info.php
[php]define(‘TEXT_THE_QUANTITY’, ‘You have %s units of this item currently in your cart.’);[/php]

includes/languages/french/product_info.php
[php]define(‘TEXT_THE_QUANTITY’, ‘Vous avez %s unité de cet item dans votre panier actuellement.’);[/php]

in product_info.php
[php]echo sprintf(TEXT_THE_QUANTITY, $cart->get_quantity($product_info[‘products_id’]));[/php]

Pretty easy!

4 thoughts on “Show quantity in cart on each product_info.php page

  1. Will

    Hey Gary,

    It’s Will here… 🙂

    You’re code is very good but for a reason, it does’nt work if I want to use it with AJAX.

    Like on my site… http://www.khe opsinternational.ca/catalog/product_info.php?products_id=1150

    Very great website BTW !
    Will

  2. Will

    Ho yeah…
    and my first code and yours won’t work with attribute…

    Someone from the french osc forum helped me and this would work with attribute:

    $superid = $product_info[‘products_id’];
    $products = $cart->get_products();
    for ($i=0, $n=sizeof($products); $i<$n; $i++) {
    $prid = tep_get_prid($products[$i][‘id’]);
    if ($prid == $superid) {
    $qtyencart = $products[$i][‘quantity’];
    echo”Vous avez $qtyencart unité de cet item dans votre panier actuellement.”;
    break;
    }
    }

    Hope this can help someone!

    Now I want to add this code also on the categorie page near each add to cart button..
    this might be harder…

    Bye

  3. Gary Post author

    No…

    echo $cart->get_quantity(tep_get_prid($product_info[‘products_id’]));

    would work (possibly) with attributes – haven’t tested. Don’t ever do more code than is necessary.

    a product with attribute is like this: product_info.php?products_id=1{4}1{3}5

    the function tep_get_prid strips out the options, to make it back to this: product_info.php?products_id=1

  4. Will

    Well just wanted to say that you’re code work with ajax too. I was wrong!

    Thank’s for your help on the forum and on your great blog.
    Will

Leave a Reply

Your email address will not be published.