0

Want to append a key and value to the already created session.

if (!isset($_SESSION['cart'])) {

                $bag = array(
                        "sessionId" => session_id(),
                        "productId" => $productId, 
                        "size"      => $productSize,
                        "quantity"  => $productQuantity
                    );

                $_SESSION['cart'] = $bag;

            } else {

                $_SESSION['cart']['sessionId'] = session_id();
                $_SESSION['cart']['productId'] = $productId;
                $_SESSION['cart']['size'] = $productSize;
                $_SESSION['cart']['quantity'] = $productQuantity;

            }

If session has already been created, then append the new variables to the session with its keys.

1 Answer 1

1

$_SESSION['cart'] should be an array of items, not a single item as you've written it. Each item will be a separate associative array, which you push onto the cart array.

if (!isset($_SESSION['cart'])) {
    $_SESSION['cart'] = array();
}
$bag = array(
    "sessionId" => session_id(),
    "productId" => $productId, 
    "size"      => $productSize,
    "quantity"  => $productQuantity
);
$_SESSION['cart'][] = $bag;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.