Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Shopping Cart : secondary price

Hi guys,

First time poster here, been using CodeIgniter for the past 6 months and I love it, it's so simple and intuitive.

I got a question however. I understand the shopping cart is a very basic library and wasn't developped to take every situation into account.

Here's what I can't seem to do: I have some products. Some of them have both a monthly fee and a unique setup fee.

The monthly fee is set in as the price in the cart. Since I didn't have any other option, I used the "options" to set the setup fee. It works like a charm, however I'm stuck when it comes to calculate the total of those setup fees.

In my view, I have tried the following, however it only ever echoes the fee of the last product that was added to the cart:

$total = 0;
   if ($this->cart->has_options($item['rowid'])) {
      foreach ($this->cart->product_options($item['rowid']) as $option => $value){
         $total += $value;
echo $total;

I know in my head the solution is simple, however I do honestly lack some coding logic when it comes to loops and the likes...

Can anybody help me on this?

Thanks in advances!

Ok, solution found by modifying the cart library myself. It works like a charm, I'll post the changes I made here, if it can help anybody who wonders how to do the same. The problem with using the 'option' parameter is that it acts as an array, not the simplest to use. I realize the modification I made is nothing complex, but for someone who's not really good in PHP, this may come as useful.

1. Create a new parameter in your controller function that handles the product additions. Example:

$insert = array(
            'id' => $this->input->post('productid'),
            'qty' => 1,
            'price' => 0,
            'setup' => 49,
            'name' => $product->name


2. Open /system/libraries/cart.php and add the following lines to the _save_cart() function (look for the /*HERE*/'s):

function _save_cart()
        // Unset these so our total can be calculated correctly below

        // Lets add up the individual prices and set the cart sub-total
        $total = 0;
        /*HERE*/$setupTotal = 0;
        foreach ($this->_cart_contents as $key => $val)
            // We make sure the array contains the proper indexes
      // do not stop unless absolutely necessary, someone might be expecting subtotal to exist later.
            if ( ! is_array($val) /*OR ! isset($val['price']) OR ! isset($val['qty'])*/)

            $total += ($val['price'] * $val['qty']);
               /*HERE*/ $setupTotal += $val['setup'] * $val['qty'];
            // Set the subtotal
            $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']);
                /*HERE*/$this->_cart_contents[$key]['setupTotal'] = ($this->_cart_contents[$key]['setup'] * $this->_cart_contents[$key]['qty']);

        // Set the cart total and total items.
        $this->_cart_contents['total_items'] = count($this->_cart_contents);            
        $this->_cart_contents['cart_total'] = $total;
        /*HERE*/$this->_cart_contents['cart_setupTotal'] = $setupTotal;

3. Still in the cart.php library, create a simple function that returns the setupTotal

function setupTotal()
        return $this->_cart_contents['cart_setupTotal'];

4. Add a couple more things to make sure CI handles the empty cart properly:

In contents() function add:


In destroy() function add:

$this->_cart_contents['cart_setupTotal'] = 0;

In CI_Cart($params = array()) function, add this line:

   // No cart exists so we'll set some base values
   $this->_cart_contents['cart_total'] = 0;
   $this->_cart_contents['cart_setupTotal'] = 0;
   $this->_cart_contents['total_items'] = 0;        
   log_message('debug', "Cart Class Initialized");

That should be it. You can now access this secondary price simply by calling:


You can of course change the names that I gave to the values and functions.


Theme © iAndrew 2016 - Forum software by © MyBB