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

[eluser]h311m4n[/eluser]
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:

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

$this->cart->insert($insert);

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

Code:
function _save_cart()
    {
        // Unset these so our total can be calculated correctly below
        unset($this->_cart_contents['total_items']);
        unset($this->_cart_contents['cart_total']);
        /*HERE*/unset($this->_cart_contents['cart_setupTotal']);

        // 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'])*/)
            {
                continue;
            }

            $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

Code:
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:

Code:
unset($cart['cart_setupTotal']);


In destroy() function add:

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


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

Code:
...
else
   {
   // 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:

Code:
$this->cart->setupTotal();

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

Cheers


Messages In This Thread
[SOLVED] Shopping Cart : secondary price - by El Forum - 03-05-2011, 09:43 AM
[SOLVED] Shopping Cart : secondary price - by El Forum - 03-05-2011, 12:34 PM



Theme © iAndrew 2016 - Forum software by © MyBB