Welcome Guest, Not a member yet? Register   Sign In
Problems with the cart class
#9

[eluser]Dule[/eluser]
Actually, this is the code of MY_Cart.php library, posted by nelson.wells, I just debugged it ($item instead of $items), and now it works fine: lets you update not only quantity, but also any other index in your cart, such as name or price.
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Cart extends CI_Cart
    {
        function __construct()
        {
            parent::CI_Cart();
        }
        
        function update_all($items = array())
        {
            // Was any cart data passed?
            if ( ! is_array($items) OR count($items) == 0)
            {
                return false;
            }
            
            // You can either update a single product using a one-dimensional array,
            // or multiple products using a multi-dimensional one.  The way we
            // determine the array type is by looking for a required array key named "rowid".
            // If it's not found we assume it's a multi-dimensional array    
            if (isset($items['rowid']))
            {    
                $this->_update_item($items);
            }
            else
            {
                foreach($items as $item)
                {
                    $this->_update_item($item);
                }
            }
            
            $this->_save_cart();
        }
        
        /*
         * Function: _update_item
         * Param: Array with a rowid and information about the item to be updated
         *             such as qty, name, price, custom fields.
         */
        function _update_item($item)
        {
            foreach($item as $key => $value)
            {
                //don't allow them to change the rowid
                if($key == 'rowid')
                {
                    continue;
                }
                
                //do some processing if qty is
                //updated since it has strict requirements
                if($key == "qty")
                {                    
                    // Prep the quantity
                    $item['qty'] = preg_replace('/([^0-9])/i', '', $item['qty']);
            
                    // Is the quantity a number?
                    if ( ! is_numeric($item['qty']))
                    {
                        continue;
                    }
                    
                    // Is the new quantity different than what is already saved in the cart?
                    // If it's the same there's nothing to do
                    if ($this->_cart_contents[$item['rowid']]['qty'] == $item['qty'])
                    {
                        continue;
                    }
            
                    // Is the quantity zero?  If so we will remove the item from the cart.
                    // If the quantity is greater than zero we are updating
                    if ($item['qty'] == 0)
                    {
                        unset($this->_cart_contents[$item['rowid']]);
                        continue;
                    }
                }
                
                $this->_cart_contents[$item['rowid']][$key] = $value;
            }
        }
    }
// END MY_Cart Class

/* End of file MY_Cart.php */
/* Location: ./system/application/libraries/MY_Cart.php */
Don't forget, instead of calling:
Code:
$this->cart->update($data);
you have to call
Code:
$this->cart->update_all($data);
Good luck!


Messages In This Thread
Problems with the cart class - by El Forum - 03-18-2010, 09:59 AM
Problems with the cart class - by El Forum - 03-25-2010, 05:58 AM
Problems with the cart class - by El Forum - 03-25-2010, 08:29 AM
Problems with the cart class - by El Forum - 03-25-2010, 08:35 AM
Problems with the cart class - by El Forum - 03-25-2010, 09:03 AM
Problems with the cart class - by El Forum - 04-02-2010, 04:35 PM
Problems with the cart class - by El Forum - 04-02-2010, 08:19 PM
Problems with the cart class - by El Forum - 04-13-2010, 10:57 PM
Problems with the cart class - by El Forum - 11-01-2010, 04:26 AM



Theme © iAndrew 2016 - Forum software by © MyBB