title: store 0 as price in Cart codeigniter class
body: <p>In my application, I have both free tickets and paid tickets in my products and user can add both free and paid tickets. Cart class won't be able to add the product if price given is 0. To add product in the cart if price is 0, I have to extend the Cart class but nothing is working out. Help appreciated. </p>
  
  <pre><code>&lt;?php
  
      class MY_Cart extends CI_Cart
      {
          function __construct()
          {
              parent::__construct();
          }
  
  
  
      function insert_item($items = array())
      {
          // Was any cart data passed? No? Bah...
          if ( ! is_array($items) OR count($items) == 0)
          {
              log_message('error', 'The insert method must be passed an array containing data.');
              return FALSE;
          }
  
          $save_cart = FALSE;
          if (isset($items['id']))
          {
              if (($rowid = $this-&gt;_insert_item($items)))
              {
                  $save_cart = TRUE;
              }
          }
          else
          {
              foreach ($items as $val)
              {
                  if (is_array($val) AND isset($val['id']))
                  {
                      if ($this-&gt;_insert_item($val))
                      {
                          $save_cart = TRUE;
                      }
                  }
              }
          }
  
          // Save the cart data if the insert was successful
          if ($save_cart == TRUE)
          {
              $this-&gt;_save_cart();
              return isset($rowid) ? $rowid : TRUE;
          }
  
          return FALSE;
      }
  
      function _insert_item($items = array())
      {
          // Was any cart data passed? No? Bah...
          if ( ! is_array($items) OR count($items) == 0)
          {
              log_message('error', 'The insert method must be passed an array containing data.');
              return FALSE;
          }
  
          // --------------------------------------------------------------------
  
          // Does the $items array contain an id, quantity, price, and name?  These are required
          if ( ! isset($items['id']) OR ! isset($items['qty']) OR ! isset($items['price']) OR ! isset($items['name']))
          {
              log_message('error', 'The cart array must contain a product ID, quantity, price, and name.');
              return FALSE;
          }
  
          // --------------------------------------------------------------------
  
          // Prep the quantity. It can only be a number.  Duh...
          $items['qty'] = trim(preg_replace('/([^0-9])/i', '', $items['qty']));
          // Trim any leading zeros
          $items['qty'] = trim(preg_replace('/(^[0]+)/i', '', $items['qty']));
  
          // If the quantity is zero or blank there's nothing for us to do
          if ( ! is_numeric($items['qty']) OR $items['qty'] == 0)
          {
              return FALSE;
          }
  
          if ( ! preg_match("/^[".$this-&gt;product_id_rules."]+$/i", $items['id']))
          {
              log_message('error', 'Invalid product ID.  The product ID can only contain alpha-numeric characters, dashes, and underscores');
              return FALSE;
          }
  
          if ( ! preg_match("/^[".$this-&gt;product_name_rules."]+$/i", $items['name']))
          {
              log_message('error', 'An invalid name was submitted as the product name: '.$items['name'].' The name can only contain alpha-numeric characters, dashes, underscores, colons, and spaces');
              return FALSE;
          }
  
          // --------------------------------------------------------------------
  
          // Prep the price.  Remove anything that isn't a number or decimal point.
          $items['price'] = trim(preg_replace('/([^0-9\.])/i', '', $items['price']));
  
          // Is the price a valid number?
          if ( ! is_numeric($items['price']))
          {
              log_message('error', 'An invalid price was submitted for product ID: '.$items['id']);
              return FALSE;
          }
  
          if (isset($items['options']) AND count($items['options']) &gt; 0)
          {
              $rowid = md5($items['id'].implode('', $items['options']));
          }
          else
          {
              $rowid = md5($items['id']);
          }
  
          // --------------------------------------------------------------------
  
          // Now that we have our unique "row ID", we'll add our cart items to the master array
  
          // let's unset this first, just to make sure our index contains only the data from this submission
          unset($this-&gt;_cart_contents[$rowid]);
  
          // Create a new index with our new row ID
          $this-&gt;_cart_contents[$rowid]['rowid'] = $rowid;
  
          // And add the new items to the cart array
          foreach ($items as $key =&gt; $val)
          {
              $this-&gt;_cart_contents[$rowid][$key] = $val;
          }
  
          // Woot!
          return $rowid;
      }
  </code></pre>