Welcome Guest, Not a member yet? Register   Sign In
Problem(bug?) with the cart class
#1

[eluser]brandon.cordell[/eluser]
I've been developing web applications for a few months, but I have a decent amount of experience implementing shopping carts. Everything from Zen cart, to OsCommerce, to Magento.

I recently implemented a custom cart solution that we built in CI. I noticed that when you try to insert an identical product, that is already in the cart rather than update the quantity of the existing cart item, it overwrites the existing product and returns true.

Every cart I have worked with has this functionality. I'm not sure if this a bug, or just an oversight, or even failure by design.

Regardless, I have modified the core cart class to fix this and would like to contribute it to the community. I searched and could not find anything, but I'm not sure if I just searched for the wrong keywords.

I'll post the code when I get home, hopefully this isn't a repost.

Thanks

Brandon
#2

[eluser]brandon.cordell[/eluser]
Here is the code I added to the end of the Cart class

Code:
/**
    * Duplicate product check
    *
    * Checks to see if a product being added is already in the cart
    * if so, increment the quantity
    *
    * @access    private
    * @params  array
    * @return    bool
    */
    function _duplicate_check($items)
    {
        if ( ! is_array($items))
        {
            log_message('debug', 'Parameters passed to _duplicate_check must be an array.');
            return FALSE;
        }

        // Check to see if this product has options, so we can
        // generate a rowid and see if it exists in our cart already
        if (isset($items['options']) AND count($items['options']) > 0)
        {
            $rowid = md5($items['id'].implode('', $items['options']));
        }
        else
        {
            $rowid = md5($items['id']);
        }

        // let's check our current cart contents to see if
        // there is a matching rowid
        foreach ($this->_cart_contents as $product)
        {
           if ($product['rowid'] == $rowid)
           {
               log_message('debug', 'Matched: '.$product['rowid']);
               return TRUE;
           }
        }

        return FALSE;
    }

Every product added to the cart is individually run passed through cart->_insert() so I figured this would be the perfect spot to use my _duplicate_check().

I've added this block of code to Cart.php.

Code:
// Let's look for duplicate products in the cart        
        if ($this->_duplicate_check($items))
        {
            // get our rowid for update
            if (isset($items['options']) AND count($items['options']) > 0)
            {
                $rowid = md5($items['id'].implode('', $items['options']));
            }
            else
            {
                $rowid = md5($items['id']);
            }

            // remember, since we have an identical item in the cart already, we have
            // to add the requested quantity to the current quantity
            $qty = $items['qty'] + $this->_cart_contents[$rowid]['qty'];

            $existing_product = array('rowid' => $rowid,
                                      'qty'   => $qty);

            if ($this->update($existing_product))
            {
                log_message('debug', 'Existing product updated.');
                return TRUE;
            }
        }

I've tested the results through a number of scenarios, but would like to continue testing to make sure this works as it supposed to.
#3

[eluser]brandon.cordell[/eluser]
Any Feedback?
#4

[eluser]jurosik[/eluser]
Hi, i'm doin the same with this little modification to _insert function

Code:
if(isset($this->_cart_contents[$rowid]))
{
    $this->_cart_contents[$rowid]['qty'] += $items['qty'];
}
else
{
    // Create a new index with our new row ID
    $this->_cart_contents[$rowid]['rowid'] = $rowid;
        
    // And add the new items to the cart array            
    foreach ($items as $key => $val)
    {
        $this->_cart_contents[$rowid][$key] = $val;
    }
}
#5

[eluser]Unknown[/eluser]
Just wanted to say Thank you for posting this code it's come in very handy.
#6

[eluser]jurosik[/eluser]
.




Theme © iAndrew 2016 - Forum software by © MyBB