In my bootstrap file where I instantiate my twig I have:
$twig->addGlobal('cart', $session->get('cart'));
and in top navbar of my twig I have a badge to show how many items are in added in cart as below:
{{ cart|length }}
and my main file that is called after bootstrap file I said above, I have:
if (!empty($_getvars['id'])) {
$data = $session->get('cart');
if(!isset($data[$_getvars['id']])) {
$data[$_getvars['id']] = 0;
}
$data[$_getvars['id']] += 1;
$session->set('cart', $data);
}
print_r($session->get('cart'));
adding to sessions is working fine, and print debug above shows that it is accurate, but in the top navbar badge I always get the previous amount of items rather than current amount unless otherwise I refresh the page to show the current. How to fix it?