CodeIgniter Forums

Full Version: Shopping Cart Entires with Duplicate IDs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]HA[/eluser]
Hello -

I'm currently building a shopping cart using the shopping cart class and I'm running into some odd behavior when trying to duplicate an entry already in the cart.

It appears if I try and insert an item into the cart with the same ID as an item that is currently in there, instead of inserting a new item it is updating the item currently in the cart with any variances I'm trying to apply to the new item.

I thought that the unique identifier for items in the shopping cart was 'rowid', which was to allow items with duplicate 'ID's but different variances.

Perhaps I'm just doing something wrong? Or is this intended behavior I somehow misunderstood? My code follows:

Code:
foreach($this->cart->contents() as $item) {        
    //match the row ID on the item we want to duplicate
    if ($item['rowid'] == $rowid) {
        
        //create a duplicate item to add to cart
        $cart = array(
            'id' => $item['id'],     //'id' => 'unique string',  will insert a new item as expected
            'qty' => 1,                                
            'price' => 'asdasd',                            
            'name' => $item['name'],                        
            'discount_id' => 'a new string',            
            
        );

        //updates current cart item $item['id'] instead of creating new record
        $this->cart->insert($cart);  
    }
}

El Forum

[eluser]HA[/eluser]
Ahh... Found the issue.

I'm not sure if it is functionally something you'd want to change in the CI core, but the reason I was having a problem was I wasn't using the optional 'options' array to store my items attributes, I was instead adding attributes directly to the items itself (which is also documented usage).

In the Cart class's _insert function (line 140) it is documented that:

Code:
// We now need to create a unique identifier for the item being inserted into the cart.
// Every time something is added to the cart it is stored in the master cart array.
// Each row in the cart array, however, must have a unique index that identifies not only
// a particular product, but makes it possible to store identical products with different options.
// For example, what if someone buys two identical t-shirts (same product ID), but in
// different sizes?  The product ID (and other attributes, like the name) will be identical for
// both sizes because it's the same shirt. The only difference will be the size.
// Internally, we need to treat identical submissions, but with different options, as a unique product.
// Our solution is to convert the options array to a string and MD5 it along with the product ID.
// This becomes the unique "row ID"

Perhaps a better implementation would be to apply a "random" string to apply MD5 to to create its 'rowid', in case the optional 'options' array isn't being used (or isn't being changed).

Of course, that would only be in the case you did indeed *always* want the user to be able to create cart items with all duplicate attributes naively!