• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[ SOLVED ] Update Price to new currency (Cart library)


My shopping cart has 3 type of currencies. When customer change currency while there are items inside the shopping cart, the cart must be able to convert the price according to the new currency. I had came out with below codes, but the prices inside the $this->cart->contents() is not updated as expected. Any help to fix this would be appreciated.

if ($this->cart->contents())
foreach ($this->cart->contents() as $items)
$rowid = $items[‘rowid’];
$id = $items[‘id’];

// get new price from database
$this->db->where(‘id’, $id);
$price = $this->db->get(‘products’)->row();

$new_price = $price->price_usd;

$data = array(
‘rowid’ => $rowid,
‘price’ => $new_price



Might sound funny, but are you getting the correct price back from the database? Your code looks correct, so I am not sure why this isn't updating except for the fact that the information is not properly being returned back.

Hi pickupman,

I had solved this problem. My code is indeed working, but the update class in CI Cart library does not update the price. It updates only the quantity. I solved it by extending the Cart class to update the price as well. No doubt the CI Cart library is quite limited in functionality out of the box. But it can be very powerful if extend it.

Thank you

Hi, I try to update cart price too.
I still new to codeigniter, could you tell me how to extend cart class?
I try to do that but no luck.

Thank in advance.

Hi nemo1112,

Could you please describe what extension you wish to have with the Cart library? In my case, I extended it to be able to calculate weight, discountable item, retrieve prefixed prices in varies currency from database when switching currency and update the cart.

Thank you.

Hi tkaw220

I want to update cart price. For my web site promotion they will checking if customer buy 2 items.
I have to change item price.


Hi nemo1112,

Are you trying to give discount on prices if the buyer purchase more than 1 item? Or you want your cart to be able to total up the price of 2 items? If you go for second case, then the Cart library bundle with the CI download has this functionality covered. Otherwise, you would have to extend the Cart library.

Go to system/libraries/Cart.php, look for the update function and you'll notice it update only the quantity of an item inside cart. Duplicate this file to application/library/ and renamed it to MY_Cart.php. Put your customize code (extend the update function to update price) in this file. Don't overwrite the Cart.php inside the system folder as it would be difficult to upgrade to newer CI version later.

Hope the above help.

Hi tkaw220

I want to do first case. Now i can do it now by use "nelson.wells-extended-codeigniter-cart"
Thank for your help tkaw220.


Hi nemo1112,

Thanks for sharing. The Nelson's extended Cart is excellent. Now I have it integrated into my system. In many cases, update quantity in cart is sufficient. But there is time where we wish to update all array items too. Now my codes are cleaner.


No problem, tkaw220 ;-)

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.