Welcome Guest, Not a member yet? Register   Sign In
Cart class update all to qty. 0 leaves one in the cart
#1

[eluser]skunkbad[/eluser]
When I have an item in the cart, and update it to qty 0 it gets deleted.

When I have multiple items in the cart, and update all to any quantity besides zero, all are updated correctly.

When I have multiple items in the cart, and update all to qty 0, one is left behind, and qty isn't changed.

Controller method handling the update:
Code:
public function update()
    {
        // for each item in the cart
        for ($i = 1; $i <= $this->cart->total_items(); $i++)
        {
            $item = $this->input->post($i);

            // if the posted qty value is a number
            if(is_numeric($item['qty']))
            {
                // set cart update array
                $data[$i] = array(
                        'rowid'    => $item['rowid'],
                        'qty'    => $item['qty']
                );

            }

            // update the cart
            $this->cart->update($data);
        }

        $this->_display();
    }

MY_Cart.php
Code:
&lt;?php
class MY_Cart extends CI_Cart {

    function MY_Cart($params = array())
    {
        // Are any config settings being passed manually?  If so, set them
        $config = array();
        if (count($params) > 0)
        {
            foreach ($params as $key => $val)
            {
                $config[$key] = $val;
            }
        }
            
        // Grab the shopping cart array from the session table, if it exists
        if (isset($_SESSION['cart_contents']))
        {
            $this->_cart_contents = $_SESSION['cart_contents'];
        }
        else
        {
            // No cart exists so we'll set some base values
            $this->_cart_contents['cart_total'] = 0;
            $this->_cart_contents['total_items'] = 0;
        }
    
        log_message('debug', "Cart Class Initialized");
    }

    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']);

        // Lets add up the individual prices and set the cart sub-total
        $total = 0;
        foreach ($this->_cart_contents as $key => $val)
        {
            // We make sure the array contains the proper indexes
            if ( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty']))
            {
                continue;
            }

            $total += ($val['price'] * $val['qty']);
            
            // Set the subtotal
            $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $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;
    
        // Is our cart empty?  If so we delete it from the session
        if (count($this->_cart_contents) <= 2)
        {
            unset($_SESSION['cart_contents']);
            
            // Nothing more to do... coffee time!
            return FALSE;
        }

        // If we made it this far it means that our cart has data.
        // Let's pass it to the Session class so it can be stored
        $_SESSION['cart_contents'] = $this->_cart_contents;

        // Woot!
        return TRUE;
    }

    function destroy()
    {
        unset($this->_cart_contents);
    
        $this->_cart_contents['cart_total'] = 0;
        $this->_cart_contents['total_items'] = 0;

        if(isset($_SESSION['cart_contents']))
        {
            unset($_SESSION['cart_contents']);
        }

        if(isset($_SESSION['checkout_data']))
        {
            unset($_SESSION['checkout_data']);
        }
    }

}
/* End of file MY_Cart.php */
/* Location: /application/libraries/MY_Cart.php */

I just stumbled upon this tonight. The rest of the cart is fully functional. I'm wondering if anyone else has this problem, or is it just me?
#2

[eluser]skunkbad[/eluser]
Can I get a confirmation? Is it just me, or is this happening to you? I fixed this by checking if all == 0, and deleting the cart, but it would be good to know if this is happening to other people.
#3

[eluser]helmutbjorg[/eluser]
I answered this is another thread

http://ellislab.com/forums/viewthread/128969/#657178
#4

[eluser]skunkbad[/eluser]
Hmm, cool. I didn't realize that the total items would be evaluated with every loop.




Theme © iAndrew 2016 - Forum software by © MyBB