0
votes

I'm writing a small shopping cart facility on top of the codeignighter framework. Using the built in cart Class, I want to update a products price that has already being added to the cart.

This is similar to a coupon at the checkout point. I'm offering $5 off the current price of a particular product.

To me the code below looks ok but it's not updating the prices.

Anyone know why?

 foreach ($this->cart->contents() as $item) {
      if($item['id'] == 1) {
          $item['price'] = $item['price'] - 5;
          $this->cart->update($item);
      }

 }
1
Well, is the product id 1? var_dump $item to see if you're checking the right IDDamien Pirsy
Yeah I always var_dump(). The data being passed is goodIEnumerable
Your using items['price'] instead of $item, unless thats a typo.Sam Bowyer

1 Answers

1
votes

For the cart->update function to work, you need to pass the rowid to it. Try:

foreach ($this->cart->contents() as $items) {
      if($items['id'] == 1) {
          $rowid = $items['rowid'];

          $data = array(
             'rowid' => $rowid,
             'price' => $items['price']-5
          );
          $this->cart->update($data);
      }

 }