5

I'm writing a custom component cart, and using a form post to save cart data in the Joomla session.

It works initially, but then if the user goes back to add another item, the original item is overwritten. The form itself is simply posting from the products view to the cart view, so I'll just show you how it's being retrieved and saved to session:

$cart = array();
$cart['items'][] = array(
        'action' => $app->input->get('action', 'add', 'STRING'),
        'domain_name' => $app->input->get('chosen_domain', '', 'STRING'),
        'chosen_plan' => $app->input->get('chosen_plan', '', 'STRING'),
        'chosen_location' => $app->input->get('chosen_location', '', 'STRING'),
        'chosen_type' => $app->input->get('chosen_type', '', 'STRING'),
        'chosen_id' => $app->input->get('chosen_id', '', 'INT'),
        'chosen_period' => $app->input->get('chosen_period', '', 'INT')
    );
$session = JFactory::getSession();
$session->set('cart', $cart);
$cart_result = $session->get('cart');

echo "<pre>" . print_r($cart_result, TRUE) . "</pre>";
5

It looks like you're creating a new cart array instead of starting with your stored data.

Try this:

$session = JFactory::getSession();
$cart = $session->get('cart',array('items'=>array()));
$cart['items'][] = array(
        'action' => $app->input->get('action', 'add', 'STRING'),
        'domain_name' => $app->input->get('chosen_domain', '', 'STRING'),
        'chosen_plan' => $app->input->get('chosen_plan', '', 'STRING'),
        'chosen_location' => $app->input->get('chosen_location', '', 'STRING'),
        'chosen_type' => $app->input->get('chosen_type', '', 'STRING'),
        'chosen_id' => $app->input->get('chosen_id', '', 'INT'),
        'chosen_period' => $app->input->get('chosen_period', '', 'INT')
    );
$session->set('cart', $cart);
$cart_result = $session->get('cart');

echo "<pre>" . print_r($cart_result, TRUE) . "</pre>";
  • 1
    Sometimes it takes a second set of eyes or a nice long distraction to find the issue. – Michael Oct 26 '14 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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