Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

item and item"s" in shopping cart


yubnub

Recommended Posts

Posted

Hi,

 

I currently have the number of items and the cart total displayed in my header.

 

I am trying to change to code so that the text reads correctly depending on how what my customer has in their cart at the time,

 

ie

 

Now in your cart 0 items

 

Now in your cart 1 item (no "s")

 

Now in your cart 2 items

 

I guess it is a fairly simple IF statement to call the correct word.

 

If anyone can point me in the correct direction or help with the code i would be most gratefull,

 

many thanks,

 

anthony

Posted

echo $cart->count_contents() == "1" ? "item" : "items";

 

Hi Burt,

 

Many thanks for that. I have taken a look at the full code on your blog and it works fine as it is.

 

However, I am trying to also style the result so the "number" (ie 1,2,3,4, and so on) of items is a different style/class from the text.

 

ie in my cart 0 items

 

How would i mod the code to do that?

 

many thaks again,

 

anthony

Posted

or do this one

 

$str_item = $cart->count_contents() <= "1" ? "item" : "items";

 

as for the style

 

<style>
#my_cart{color:#(color you want)}
#cart_count_items{color:#(color you want)}
#items{color:#(color you want)}
</style>
<div id="my_cart">in my cart</div> <div id="cart_count_items"><?=$cart->count_contents()?></div> <div id="items"><?=$str_item?></div>

Posted

Something to keep in mind, if this cart is ever used in a language other than English, is that which singlular/plural item/items (translated) to use may have different criteria for different languages, and may even have more than two forms. Not to mention that the sentence syntax may be different ("items 0" in some languages, instead of "0 items"). Unfortunately, in the language-definition files (english.php, etc.) you need to confirm that you have the information at hand (cart contents) to select the right form in the language file. If not, it will have to be pulled out into the main code, with a different section for each language used. Just something to keep in mind if you're ever going to support multiple languages -- they don't necessarily have the same grammar rules as English.

Posted

Phil - that's a good point.

 

How about using a sprintf per language? Then we have no reason to worry about any "lost in translations"...

 

I haven't tried, but it should be very straightforward!

 

define('TEXT_IN_CART', 'You have %s %s in your cart, totalling %s');
define('TEXT_ITEM_1', 'item ');
define('TEXT_ITEM_NOT_1', 'items ');

 

change for different languages as appropriate

 

Then, something like

 

echo sprintf(TEXT_IN_CART, $cart->count_contents(), $cart->count_contents()== "1" ? TEXT_ITEM_1 : TEXT_ITEM_NOT_1 , $cart->display_total());

 

Note all the above off the top of my head, so may not work as intended. or amy not work at all. But I'm sure you get the idea

Posted

Well, one of my points was that some language might use singular (item) for both 0 and 1, and plural (items) for 2+. So, the check might be different for different languages, not just 1 or not 1 (as in English). Such checks are language-specific. If all the relevant data is known at the time the language file is included, such code could be in the language file. Otherwise it needs to be in the main code, or possibly the language file could define a function for the main code to call. E.g., how_many_items($cart->contents()) returns "0 items" or "1 item" or "2 items" in English, but in East Trovakian it returns "itine 0" or "itine 1" or "itinezic 2". That's one way to do it. You just have to avoid hardcoding to English grammatical conventions.

  • 1 month later...
Posted

echo $cart->count_contents() == "1" ? "item" : "items";

 

Hi Burt,

 

sorry for thelate reply to this old thread.

 

I am almost there with the new site design, though this item(s) issue is still giving me grief.

 

I have modded the design of the site and have the shopping cart info in a info box in column right.

 

I have tried using the code suggested though sadly it does not work.

 

the code below is the std code I have

 

Now in your cart <b>'.sizeof($products = $cart->get_products()).' items</b></span>';

 

I have tried different variations of the 2 sets of code but cant suss it,

 

any ideas?

 

many thanks

Posted

First of all, I think you want to use $cart->count_contents() instead of sizeof($products = $cart->get_products() to get the number of items. If your store will never be in any language other than English, go ahead and hardcode it as

Now in your cart <b>'.$cart->count_contents().' item'.(($cart->count_contents()==1)?'':'s').'</b></span>';

If you ever might support non-English languages:

$text['now_in_your_cart']. '<b>'.n_items($cart->count_contents()).'</b></span>';

and define a function in each language file. For the English language file:

// English: 1 item / n items (for n not 1)
function n_items($count) {
 $output = $count . ' item';
 if ($count != 1) $output .= 's';
 return $output;
}

For the Upper Elbonian language file:

// Upper Elbonian: shika 0 / shikasa 1 / shikashikaboomboom n (for n > 1)
function n_items($count) {
 if ($count == 0) {
    $output = 'shika';
 } else if ($count == 1) {
    $output = 'shikasa';
 } else {
    $output = 'shikashikaboomboom';
 } 
 $output .= ' ' . $count;
 return $output;
}

Posted

First of all, I think you want to use $cart->count_contents() instead of sizeof($products = $cart->get_products() to get the number of items. If your store will never be in any language other than English, go ahead and hardcode it as

Now in your cart <b>'.$cart->count_contents().' item'.(($cart->count_contents()==1)?'':'s').'</b></span>';

If you ever might support non-English languages:

$text['now_in_your_cart']. '<b>'.n_items($cart->count_contents()).'</b></span>';

and define a function in each language file. For the English language file:

// English: 1 item / n items (for n not 1)
function n_items($count) {
 $output = $count . ' item';
 if ($count != 1) $output .= 's';
 return $output;
}

For the Upper Elbonian language file:

// Upper Elbonian: shika 0 / shikasa 1 / shikashikaboomboom n (for n > 1)
function n_items($count) {
 if ($count == 0) {
    $output = 'shika';
 } else if ($count == 1) {
    $output = 'shikasa';
 } else {
    $output = 'shikashikaboomboom';
 } 
 $output .= ' ' . $count;
 return $output;
}

 

Thank you so much Mr Phill - works perfectly.

 

When i first asked the question the design had the cart info displayed in the header - at that point Burts code worked perfect.

 

As the design evolved we placed it into column right - at this point the code broke.

 

Your code has done the job nicely.

 

I must admit we are never going to be a multi currency or language site - or if we ever become one this will be the least of our worries as i never add any other language files other than english when i add a contrib!

 

I suppose becoming a store of that size would be a "nice" problem to have,

 

my genuine thanks again for you help - and indeed to the other posters before,

 

best regards,

 

anthony

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...