Part of the EllisLab Network
   
 
Question about CI shopping cart class code
Posted: 07 July 2009 02:28 PM   [ Ignore ]  
Lab Assistant
RankRank
Total Posts:  217
Joined  12-19-2008

A question on the cart class.

In the class, the function _save_cart() contains the following code:

...
// Is our cart empty?  If so we delete it from the session
        
if (count ($this->_cart_contents) <= 2)
        
{
            $this
->CI->session->unset_userdata ('cart_contents');
            
            
// Nothing more to do... coffee time!
            
return FALSE;
        
}
... 

Why is the cart content validated against count <= 2 instead of count < 1?

It would appear that the save_cart function would remove any order in the session containing ZERO, ONE or TWO items. The comment makes reference to a check for an empty cart, but the code tests for <= 2 items…

Could someone please explain it?
Thanks
Dave

 Signature 

CI 2.1.0, Linux, LAMP. We also like Gold and Silver…
Find me on Freelancer.com
Verbosity does not imply erudition,
Emphasis is no guarantee of veracity.
User Guide? Nobody told me there was a User Guide!

Profile
 
 
Posted: 07 July 2009 02:34 PM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2674
Joined  05-18-2008

If you take a look at the constructor, you’ll see this block of code

// Grab the shopping cart array from the session table, if it exists
if ($this->CI->session->userdata('cart_contents') !== FALSE)
{
    $this
->_cart_contents $this->CI->session->userdata('cart_contents');
}
else
{
    
// No cart exists so we'll set some base values
    
$this->_cart_contents['cart_total'0;        
    
$this->_cart_contents['total_items'0;        

So you’ll always have those 2 items there, which are just meta data

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

Profile
 
 
Posted: 07 July 2009 02:39 PM   [ Ignore ]   [ # 2 ]  
Lab Assistant
RankRank
Total Posts:  217
Joined  12-19-2008

Dam1an,
Thanks, it makes sense now!

 Signature 

CI 2.1.0, Linux, LAMP. We also like Gold and Silver…
Find me on Freelancer.com
Verbosity does not imply erudition,
Emphasis is no guarantee of veracity.
User Guide? Nobody told me there was a User Guide!

Profile
 
 
Posted: 07 July 2009 03:10 PM   [ Ignore ]   [ # 3 ]  
Lab Assistant
RankRank
Total Posts:  211
Joined  10-22-2003

It doesn’t make sense to me.  That seems like a terrible way to use that array IMO.

 Signature 

Template Driven PHP Shopping Cart Software

Profile
 
 
Posted: 07 July 2009 03:20 PM   [ Ignore ]   [ # 4 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2674
Joined  05-18-2008

Yeah it’s not how I would have done it personally, but I’m sure the EL team have a good reason for doing it that way instead of 2 extra variables, as it’s not the way that first comes to mind (at least not for me) so there must be a great reson behind it smile

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

Profile
 
 
Posted: 07 July 2009 03:28 PM   [ Ignore ]   [ # 5 ]  
Lab Assistant
RankRank
Total Posts:  217
Joined  12-19-2008

Dam1an,
  How would you have approached this (the cart information array)?

  I’ve moded the cart class to use other currencies so, I’m more than willing to change the way it handles data.

  Is there a way to reduce the amount of info sent to the session?  Those totals seem redundant, since they can be calculated when needed, with small overhead in processor time.

 Signature 

CI 2.1.0, Linux, LAMP. We also like Gold and Silver…
Find me on Freelancer.com
Verbosity does not imply erudition,
Emphasis is no guarantee of veracity.
User Guide? Nobody told me there was a User Guide!

Profile
 
 
Posted: 07 July 2009 04:02 PM   [ Ignore ]   [ # 6 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2674
Joined  05-18-2008

It depends how big you expect the cart to get?
If in most cases it will have less then, say 10 items, I’d calculate it on the fly.
If it’s something where I expect people to have 50-100+ items (such as online groceries) I’d probably want to store it in a seperate class variable

Another thing to bear in mind would be how much traffic you’ll be getting… if you will constantly be under heavy load (unlikely) that small cost of calculating the cost/size of your cart on each page load might become an issue

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

Profile
 
 
Posted: 07 July 2009 04:37 PM   [ Ignore ]   [ # 7 ]  
Lab Assistant
RankRank
Total Posts:  217
Joined  12-19-2008

Dam1an,
Your points are valid. I also note that, depending on which session library you use, the limit on session size (4KB) is a limit on the number of items per cart.  The standard PHP session has this limit while the CI session can use a db to hold the bulk of session data at a price in server time. It’s a question of balancing size vs speed.

  I do prefer a pure db method, since it doesn’t require that cookies be enabled on the user’s machine, but I confess I use the CI cart which doesn’t support db only data storage.  Perhaps in the future I’ll create such a cart class, but I also need to sleep occasionally… Maybe in my next life.

Does anyone know of a cart that doesn’t use cookies? Preferrably open source.

 Signature 

CI 2.1.0, Linux, LAMP. We also like Gold and Silver…
Find me on Freelancer.com
Verbosity does not imply erudition,
Emphasis is no guarantee of veracity.
User Guide? Nobody told me there was a User Guide!

Profile
 
 
Posted: 08 July 2009 09:31 AM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  535
Joined  03-13-2008

wait, CI has a cart library? since when?

 Signature 

:wq

Profile
 
 
Posted: 08 July 2009 09:57 AM   [ Ignore ]   [ # 9 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2674
Joined  05-18-2008
GSV Sleeper Service - 08 July 2009 01:31 PM

wait, CI has a cart library? since when?

I’m not sure if thats a genuine or sarcastic question lol raspberry
It’s in the [URL=http://dev.ellislab.com/svn/CodeIgniter/trunk/system/libraries/Cart.php]SVN trunk[/URL]

 Signature 

I’m building a Project Management System for my 3rd year Uni project, Sign up to the beta
Track my progress | Post of the day: UI Designs
Get full auto complete support for CodeIgniter in Eclipse

Profile
 
 
Posted: 08 July 2009 02:35 PM   [ Ignore ]   [ # 10 ]  
Lab Assistant
RankRank
Total Posts:  217
Joined  12-19-2008
GSV Sleeper Service - 08 July 2009 01:31 PM

wait, CI has a cart library? since when?

In case you’re serious… The CI cart (in the SVN trunk) is a good basic design and fairly easy to modify if needed.

  I needed to support gold and silver currency, denominated in goldgrams and silver ounces respectively, to three decimal places.  It was a simple couple of hours work, over a couple of days, to go through the code and add the changes. I also added a shipping charge denominated in goldgrams and another in silver ounces, plus I changed the “format_number” function to accept a decimal places variable and remove the comma separator.

I’ll send a copy of my modified cart to anyone interested in using the gold or silver currencies.  I left intact the standard, 2 decimal place currency unit, in case you are forced to use the fiat currencies such as US$, Euro, etc.

One warning, I did NOT add the shipping variable for fiat currency, since I don’t use it. Which means that you either need to go through the code and add a 2 decimal place shipping charge variable for fiat, or place your shipping charge in the “options” area.
Dave

 Signature 

CI 2.1.0, Linux, LAMP. We also like Gold and Silver…
Find me on Freelancer.com
Verbosity does not imply erudition,
Emphasis is no guarantee of veracity.
User Guide? Nobody told me there was a User Guide!

Profile
 
 
Posted: 09 July 2009 05:59 AM   [ Ignore ]   [ # 11 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  535
Joined  03-13-2008
Dam1an - 08 July 2009 01:57 PM

I’m not sure if thats a genuine or sarcastic question lol raspberry
It’s in the [URL=http://dev.ellislab.com/svn/CodeIgniter/trunk/system/libraries/Cart.php]SVN trunk[/URL]

it was a serious question, but thanks for the sarcastic reply.

I agree with jondolar, this part seems a bit daft.

// Is our cart empty?  If so we delete it from the session
if (count($this->_cart_contents) <= 2)
... 
 Signature 

:wq

Profile