Problems with the cart class

#1
[eluser]Brayan[/eluser]
I have 2 problems:

1 . I can’t add products that names have slashes to the cart.. they are not added
other products that dont have slashes are usually added


2. On the cart->update(), I want to update the price of the product too, but it doesn't work
I only can update the quantity ? on the user guide its just quantity that they show on example :-(


There is a way to solve this problems ?

thank's a lot and sorry for the bad english =\

#2
[eluser]scud[/eluser]
I've got similar problem with cart. If I could not solve it I try to you my own library. Probably it's the best one for me.

#3
[eluser]nelson.wells[/eluser]
Look at the cart class around line 141 and find this function definition

Code:
function _insert($items = array())

That function contains rules for adding items to the cart, and the comments describe how to change the rules.

I can't really help you with the second problem right now as I have never ran into it and don't have time. I'll try to look at it when I get home tonight. Good luck.

#4
[eluser]scud[/eluser]
Great thanks for your help.
I've looked around find some advices. Now I try to add a helper to cart

#5
[eluser]Brayan[/eluser]
Thanks !

but the 1st problem I already solved..

I just commented these lines:

Code:
if ( ! preg_match("/^[".$this->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;
        }

now will add any product

anyway.. the second problem still alive =\

#6
[eluser]nelson.wells[/eluser]
After looking at the cart->update function, it will only update the quantity. Does anyone know if they plan on making that function more flexible in CI 2? I don't like that the update function is only useful for updating the price. If they don't plan on making it more flexible, maybe I will lend my hand at it...

Here is a quick and dirty solution. You should take more validation and error checking steps, but this will get the job done in a hurry.

Code:
class MY_Cart extends CI_Cart
    {
        function update_price($items = array())
        {
            //only update the cart if the rowid and price keys were passed
            if( isset($items['rowid']) && isset($items['price']) )
            {
                $this->_cart_contents[$items['rowid']]['price'] = $items['price'];
                
                $this->_save_cart();
                
                return true;
            }
            else
            {
                 return false;
            }
        }
    }

Put that in the file MY_Cart.php in the libraries folder. You call it like this

Code:
$data = array(
        'rowid' => $rowid,
        'price' => $new_price
);

$this->cart->update_price($data);

Please note that the function does not accept an array of items. You'll have to do some additional programming to do that Smile

#7
[eluser]Brayan[/eluser]
thanks for your help nelson

I saw a tutorial today on nettuts and i was thinking on do what you did.. LoL

u save a lot of time .. this work's nicely.. I will add validation and other stuff.. then I'll post here..

seeya

[]'s

#8
[eluser]nelson.wells[/eluser]
I've written a better solution that someone may be interested in. This will let you update any index in the cart class (that is not an array, it doesn't do that yet). You can follow progress at the BitBucket project or read more about it on my blog.

Code:
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
                    $items['qty'] = preg_replace('/([^0-9])/i', '', $items['qty']);
            
                    // Is the quantity a number?
                    if ( ! is_numeric($items['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[$items['rowid']]['qty'] == $items['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 ($items['qty'] == 0)
                    {
                        unset($this->_cart_contents[$items['rowid']]);
                        continue;
                    }
                }
                
                $this->_cart_contents[$item['rowid']][$key] = $value;
            }
        }
    }

#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!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.