1

I use the following code in my controller for adding product items to the session:

$app = JFactory::getApplication();
$input = $app->input;
$itemid = $input->get('itemid');
$quan = $input->get('quan');
$session = JFactory::getSession();
$cart = $session->get('cart', array());
$cart['items'][] = array('itemid' => $itemid, 'quan' => $quan);
$session->set('cart', $cart);

It works, but if I add the same product with the same ID to the session once more it doesn't update the quantity of such product but adds the same product as new item. So I need something like:

if($itemid == 'itemid') {
    $cart['items'][] = array('quan' => 'quan'+$quan); // i.e. current quan + new quan
} else {
    $cart['items'][] = array('itemid' => $itemid, 'quan' => $quan);
}

It doesn't work of course

3
  • Don't have time to answer, but since PHP array is and ordered map, can't you just make the items array a map from 'itemid' => 'quan'? This map would be trivial to search and update by itemid. I assume you had some reason for adding the extra layer of data structure, though? Commented May 8, 2015 at 9:45
  • @Ville Niemi: yes that was exactly my suggestion too. I also have an example how to update that map in my answer...
    – fruppel
    Commented May 8, 2015 at 9:48
  • @fruppel Guess I should have checked your answer for that update... Well, no harm done. Commented May 8, 2015 at 9:50

1 Answer 1

3

Solution A: Slightly adjusted array structure

You have only your itemid and the corresponding quantity in your array. Why don't you use the itemid as array index ($cart['items'][<ITEMID>]) so you could easily update your quantity if the index already exists.

$cart['items'][100] = 10
$cart['items'][236] = 5
  your itemid --^     ^-- your quantity

Something like

if (isset($cart['items'][$itemid]))
{
    $cart['items'][$itemid] = $cart['items'][$itemid] + $quan;
}
else 
{
    $cart['items'][$itemid] = $quan;
}

Solution B: Loop through existing array structure

Otherwise you have to loop through your array, find the item with the right itemid and update its quantity, like this:

$app = JFactory::getApplication();
$input = $app->input;
$itemid = $input->get('itemid');
$quan = $input->get('quan');
$session = JFactory::getSession();
$cart = $session->get('cart', array());
$found = false;

if (isset($cart['items']) && !empty($cart['items']))
{
    foreach ($cart['items'] as &$item)
    {
        if ($item['itemid'] == $itemid)
        {
            $item['quan'] += $quan;
            $found = true;
            break;
        }
    }
}

if (!$found) 
{
    $cart['items'][] = array('itemid' => $itemid, 'quan' => $quan);
}

$session->set('cart', $cart);
2
  • When I try solution A category of products is being added to the session instead of product and I get the following record in my session: s:4:"cart";a:1:{s:5:"items";a:2:{i:2;i:2;i:3;s:1:"1";}}} (category has ID 1 and is stored in the same table, probably solution A adds incorrect ID i.e. itemid)
    – stckvrw
    Commented May 8, 2015 at 10:18
  • It's more a general idea how one could use a map to achieve the same easier than a ready-to-use solution. I do not know the details of your code and it could be that you have to modify other parts of your code as well for solution A. If solution B works for you, just go with it :-)
    – fruppel
    Commented May 8, 2015 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.