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

#1
[eluser]tkaw220[/eluser]
Hi,

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->select(‘price_usd’);
$this->db->where(‘id’, $id);
$price = $this->db->get(‘products’)->row();

$new_price = $price->price_usd;

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

$this->cart->update($data);
}
}

Thanks.

#2
[eluser]pickupman[/eluser]
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.

#3
[eluser]tkaw220[/eluser]
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

#4
[eluser]nemo1112[/eluser]
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.

#5
[eluser]tkaw220[/eluser]
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.

#6
[eluser]nemo1112[/eluser]
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.

Thankyou.

#7
[eluser]tkaw220[/eluser]
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.

#8
[eluser]nemo1112[/eluser]
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.

:-)

#9
[eluser]tkaw220[/eluser]
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.

Thanks.

#10
[eluser]nemo1112[/eluser]
No problem, tkaw220 ;-)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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