0

I have an old order system in symfony that has the In-Cart array something like this:

    Array
(
    [333] => Array
        (
            [product] => Product Object (...)
            [qty] = 1
            [criteria]
        )

)

Where 333 is the product id thats already in the cart + its characteristics.

Now the problem comes when lets say the user adds into the cart the same product but with a different criteria.

I made something like this to differentiate:

if((array_key_exists($product->getId(), $products)) AND ($products[$product->getId()]['criteria'] == $criteria))  
     {

       $products[$product->getId()]['qty'] = $products[$product->getId()]['qty'] + $qty;  


     }else{
 // todo == I can't find any solution to this :(
}

What above does is:

  • checks for the product id already being in the cart

  • checks if criteria sent matches with the one already in the cart

  • if true true => adds quantity to the In-Cart product

As you can probably already tell , if the user would add THE SAME product with different criterias , it would end up in the else.

Resume: How can I have a key with the same ID in the same array but with different values?

Any ideas would be appreciated.

Thank you!

an4rei
  • 57
  • 1
  • 7
  • 2
    Couldn't you create a key by the product id and the criteria? So that you have something like a _hash_ for each combination, like `333_criteria`. That would solve the whole problem and would remove your workaround too. – eisbehr Jun 28 '18 at 15:08
  • Or, as another idea, add multiple arrays inside of the `333` key, so that this represent the product id and you have each version with different criterias as sub-array. I think the main _problem_ here is just to rearrange your data. But this is a possible task, imo. – eisbehr Jun 28 '18 at 15:13
  • Or don't use a generated key at all. Let the `333` just be another property of the collection. `array[0] => ['productId => 333, product => obj, ...]; array[1] = [productId = 333, product => obj, ...]` – waterloomatt Jun 28 '18 at 15:15
  • @eisbehr lets say I would do it that way , how can I strip the hash from the array key later for proper database queries about picture etc.? Thank you – an4rei Jun 28 '18 at 15:27
  • @waterloomatt how could I update a property of the a specific product thats already in the cart? Lets say I have in collection 2 entries , one with productId=333 and one with productId=225. How could I set the QTY inside of the array where productID 225 is as I can't use any keys. EDIT: would array_search be the best thing? – an4rei Jun 28 '18 at 15:33
  • @an4rei - yes, `array_seach` would work well - https://stackoverflow.com/a/24527099/296555. BTW - I'm not saying this is the best way; it's just another option. A completely different approach would be to store your cart items in a DB and associate it with the user. Finally, you could use any of the above approaches in an OOP approach by creating a `CartItem` object which has all the properties you need. Your cart would then just be a collection of `CartItem`s which you would query in a similar fashion. `$item = new CartItem(productId, quantity, criteria)`. `$cart = [$item, $item, $item];`. – waterloomatt Jun 28 '18 at 17:41

1 Answers1

0

Given that each set of criteria can have it's own quantity, you can implement an array like this:

Array
(
    [333] => Array
        (
            [product] => Product Object (...)
            [criterias] => Array
                (
                    [criteria1] => 1
                    [criteria2] => 16
                )
        )
)
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77