0
votes

I successfully created simple web app with cart.

Now i need to implement some special offers.

Example 1: if customer added 2 pieces of some product - second piece should cost 30% less.

Example 2: if customer add product_1 and product_2 - then I have to add product 3 for free.

There is no problem with implementing bussiness logic itself.

The problem is, that I have no product detailed information in Codeigniter Cart.

There are id, name, qty, price, subtotal and options fields in item object/array.

To recalculate prices without querying database on every cart change I need some additional fields, something like group/category, which tell me what special offer applies to that product.

Additionally - i need to store base price somewhere, because when customer remove some product from cart - i will need to put old prices there.

Where I should put that additional informations?

options field is already used...

I don't want to query database on every cart update (i want my web application to be very fast and I want minimum server load).

Shall i expect some problems with these rowid's, when i change price of some cart item?

Im asking about this, because cart unique 'rowid' is an md5 checksum of item id and options array. My application will use diffrent prices for same product with same options (one piece normal price, another 70% price for example).

2

2 Answers

1
votes

Codeigniter cart will get you started, but for any kind of customizing, i recommend doing your own cart, will be much easier. Remember all a cart has to do is store the Product ID, and the Qty. Thats it. Everything else is for the purpose of Viewing the Cart, and Creating the Order. The order totals are separate from the cart, because for most orders there will be other charges like shipping / tax. and/or there might be promotions which change the final price.

So you might decide to include the Product name in the Cart to make it easier to display. And storing the ship weight is reasonable. But even storing the price in the cart can be dangerous. What if the price is raised - or lowered - while the shopper is shopping? These might be edge cases, but it shows you how 'stale' the information in a cart can get.

The example you cited is great because you very quickly see - the cart is built to track one line item at a time. For any kind of logic like - if you buy 3 of these, you get the third one for free - you could build that kind of logic into the cart - but its going to become messy spaghetti very quickly!

so what to do? get the cart object - with your cart items - and pass that to a promotion model. the promotion model loops through all the items in the cart - and THEN makes the adjustments - which are passed to your order methods. by having a promotion model, we are keeping the business rules for promotions separate from the cart. Most importantly

we are not changing anything in the cart !

we are applying the promotion to the items in the cart. much more flexible, and its easy to show the person how much money they saved, because you havent tried to rewrite the original cart pricing.

0
votes

I realized, that options can be array of arrays and i can store any additional data there.

I will use that array and I think there is no need to extend standard Cart library.