CodeIgniter Forums
$this->cart->total() - Can this be manipulated? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: $this->cart->total() - Can this be manipulated? (/showthread.php?tid=39483)



$this->cart->total() - Can this be manipulated? - El Forum - 03-11-2011

[eluser]EdgeTube[/eluser]
Hi all,

I am trying to create a coupon code function using the shopping cart class. For some reason I cannot manipulate the $this->cart->total() as I am given an error "Can't use method return value in write context."

Any ideas on how I could apply a coupon code and somehow change the total?

Thanks!


$this->cart->total() - Can this be manipulated? - El Forum - 03-11-2011

[eluser]bubbafoley[/eluser]
The cart class is very incomplete.

I would extend the Cart class to make the desired changes.

Something like this should work:
application/libraries/MY_Cart.php
Code:
<?php

class MY_Cart extends CI_Cart {

    function __construct()
    {
        parent::__construct();
    }

    function update_total($new_total)
    {
        $this->_cart_contents['cart_total'] = $new_total;
    }

}

then in your controller you can do this:

Code:
$this->cart->update_total(199.95);



$this->cart->total() - Can this be manipulated? - El Forum - 03-16-2011

[eluser]EdgeTube[/eluser]
Cool, thanks.

The function above seems to work well. However, it looks like it's not correctly updating the grand total.

When I use my 'coupon' controller to update the total using the function above, and echo $this->cart->total(), the price shows up correctly.

However, when I have it redirect back to the shopping cart, it looks like the grand total did not update.

Any suggestions as to how to make a permanent change to the grand total?

Thanks!