Welcome Guest, Not a member yet? Register   Sign In
changing options for cart items
#1

[eluser]zcorbitt[/eluser]
Is there a way to change the options for a cart while maintaining the items position in the cart array? Because the item key is made of id and the options for the item I don't see how I can change the options but allow the item to maintain it's position in the cart.

for example:

item 1: name=>shirt1, options(color=>red, size=>small)
item 2: name=>shirt2, options(color=>blue, size=>large)

but if I want to change item 1 to a medium the only way i see to do that is drop item 1 and add it back to the cart; which puts it at the end of the array. Is there a way to maintain the order of items? All that I can come up with is dropping all and adding them back in the order they were displayed and submitted from a cart view.

thanks
#2

[eluser]Damien K.[/eluser]
You reference the item at its "index" (whether it is by integer or key) and change the necessary options. I don't see why there is a problem with modifying the item in-place.
#3

[eluser]zcorbitt[/eluser]
It is a problem because the key is made up of the item id and the options so if I change the options the key is no longer consistent with its original makeup.

So if a customer
1. adds an item with a set of options
2. then changes the options to that item
3. then decides to add the same item back to the cart with the first set of options it will add the quantity to the existing key; however, it now has different options.

I am thinking in terms of an event registration where the item has a first name, last name and email as options. However, the registration id is the same but the first name, last name and email need to be able to change throughout the process without affecting the order of registrations because I want to keep them in order for the customers' sake.

I guess one option I have is to ensure that the cart item index is unique by adding another variable to the existing key and not concern myself with the id and options matching the key.
#4

[eluser]Damien K.[/eluser]
I'm curious how you're representing your cart items. You're using an array and the key contains (volatile) data such as options. What I'm curious is the content the key is referring to, if anything. What do each of these keys point to?

To address your case, I believe a popular approach in the industry is via OOP and using the appropriate data structures. One approach using this method is to have an CartItemList object where the implementation of this list will preserve order of added items. Then each cart item in the list will be represented as a CartItem object. CartItem will then have fields for options, quantity, and all the other required fields you need.
#5

[eluser]zcorbitt[/eluser]
Maybe I am not reading the Cart library correctly starting at line 208:

// 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"
if (isset($items['options']) AND count($items['options']) > 0)
{
$rowid = md5($items['id'].implode('', $items['options']));
}
else
{
// No options were submitted so we simply MD5 the product ID.
// Technically, we don't need to MD5 the ID in this case, but it makes
// sense to standardize the format of array indexes for both conditions
$rowid = md5($items['id']);
}
#6

[eluser]Damien K.[/eluser]
Oh.. my bad. I forgot there is a CI Cart Library. I had the impression that you did your own cart library. My apology for the confusion. That appears to be an "interesting" implementation of a cart library (or not). Anyway, I have no knowledge of the CI library...




Theme © iAndrew 2016 - Forum software by © MyBB