About Club osCommerce

Showcasing osCommerce…the good, the bad and the ugly!

StumbleUpon It! DIGG It!

Show quantity in cart on each product_info.php page

Written By Gary on Jul 21 2009 · Comments (4)

Buy Gary A Beer?
Buying me a "beer" helps me to keep my contributions updated and keep this blog alive - and you get a link from my homepage to your site. Cheers!


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:
  1. $superid = $product_info['products_id'];
  2. $products = $cart->get_products();
  3. for ($i=0, $n=sizeof($products); $i<$n; $i++) {
  4. if ($products[$i]['id'] == $superid) {
  5. $qtyencart = $products[$i]['quantity'];
  6. echo"Vous avez <b>$qtyencart unité</b> de cet item dans votre panier actuellement.";
  7. break;
  8. }
  9. }

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

PHP:
  1. echo $cart->get_quantity($product_info['products_id']);

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:
  1. define('TEXT_THE_QUANTITY', 'You have %s units of this item currently in your cart.');

includes/languages/french/product_info.php

PHP:
  1. define('TEXT_THE_QUANTITY', 'Vous avez %s unité de cet item dans votre panier actuellement.');

in product_info.php

PHP:
  1. echo sprintf(TEXT_THE_QUANTITY, $cart->get_quantity($product_info['products_id']));

Pretty easy!

4 Comments

  1. 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

    Comment by Will — July 28, 2009 @ 8:12 am


  2. 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

    Comment by Will — July 28, 2009 @ 8:18 am


  3. 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

    Comment by Gary — July 28, 2009 @ 8:24 am


  4. 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

    Comment by Will — July 29, 2009 @ 5:40 am


Leave a comment

RSS feed for comments on this post · TrackBack URL

Hot 100 osCommerce Shops

View the osCommerce HOT 100These are the best looking, most exceptional osCommerce Stores as voted for by you.

New to osCommerce - get inspiration from these beautiful shops. Reckon your site has what it takes to become a member of the HOT 100? Submit it!