Welcome Guest, Not a member yet? Register   Sign In
[ My codes appended ] 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]Блум[/eluser]
Due to my tests, the only thing you can update in a cart entry is the quantity. So I'm doing it this way: pulling everything out of the cart, update the price, (destroying the old cart information), recording everything back in the cart.

Code:
$currency_ratio = 0.5112;

// temporary array
$cart_updated = array();
foreach ($this->cart->contents() as $cart_element)
{
   // set the price in the new currency
   $cart_element['price'] = number_format( $currency_ratio * $cart_element['price'], 2, '.','');
  
   // record the currency code in the cart element
   $cart_element['options']['currency'] = 2;
   // record the updated element in a temporary array
   array_push($cart_updated, $cart_element);                    
}
// destroy the old cart
$this->cart->destroy();
// set up the updated one
$this->cart->insert($cart_updated);

It is not very elegant workaround, at the end the Cart class is very basic instrument, IMHO it needs some more flexibility, but not too much.
#3

[eluser]tkaw220[/eluser]
Hi Блум,

Thanks for your post. I had went through the entire Cart Library and found that it is very limited especially the update method update only the quantity inside the cart. Since price of my product in other currencies are preset in database rather than convert by exchange ratio, and I need the cart to handle other things such as weight, discountable item, free shipping when certain condition is met, I decided to expand the Cart Library and it worked.
#4

[eluser]Блум[/eluser]
One update to the upper code:

Code:
$currency_ratio = 0.5112;

// temporary array
$cart_updated = array();
foreach ($this->cart->contents() as $cart_element)
{
   // set the price in the new currency
   $cart_element['price'] = number_format( $currency_ratio * $cart_element['price'], 2, '.','');
  
   // record the currency code in the cart element
   $cart_element['options']['currency'] = 2;

   // remove the rowid, so the cart class can make new one that will match the key
   unset($cart_element['rowid']);

   // record the updated element in a temporary array
   array_push($cart_updated, $cart_element);                    
}
// destroy the old cart
$this->cart->destroy();
// set up the updated one
$this->cart->insert($cart_updated);

If you edit options of the cart element, you must remove the rowid from the array element, so the cart class will generate new one that will match the key in the array.
#5

[eluser]tkaw220[/eluser]
Thank you very much for the update.




Theme © iAndrew 2016 - Forum software by © MyBB