Jump to content

Imploding data from multiple sessions


padams

Recommended Posts

I've got multiple session variables called $_SESSION['cart'][$productID], with data in each called 'stylename', 'quantity' and 'price'. For example, there will be $_SESSION['cart'][1], $_SESSION['cart'][2], etc.

 

Is there any way to combine data from each into one variable? So I could have "productID1, stylename1, quantity1 and product2, stylename2, quantity2", etc.

 

 

Link to comment
Share on other sites

I'm just not sure how to best store the information. I've got an array called $_SESSION['cart'][$productID], but whenever a new item is added we need to check if the product has already been purchased so we can increment quantity by 1. That said, the style is a complicating matter as if the product has already been ordered but it is a different style, then it should become a new item in the shopping cart.

 

I just don't know how to best store the information, any ideas?

Link to comment
Share on other sites

The current session storage method using sessions is fine.

In terms of quantities you just need to check if the product Id already exists as an array key. If so increment the quantity key by 1

 

// the product is added to the cart via a form post
$productId = $_POST['productId'];
// check if it exists in the cart
if(array_key_exists($productId, $_SESSION['cart'])) {
// update the quantity
$_SESSION['cart'][$productId]['quantity'] = $_SESSION['cart'][$productId]['quantity']+1,
}
else {
  // add the new product to the cart
}

Link to comment
Share on other sites

By style im guessing you mean i.e. colour, size, etc (different product options)

If the styles are related to the productId then add the styles in a sub array i.e.

 

$_SESSION['cart'][1] = array('name' => product xyz, 'qty' => 2, styles => array('red' => 1, 'blue' => 1));

 

So the user has added 2 of product xyz to the cart,1 red and 1 blue.

 

Have you inherited this code from somewhere as the cart model seems sound and the fact you are struggling suggests this. If I were you I would break down each user action into smaller steps and tackle each one i.e.

 

User adds item to cart -

1. Check the product is not already in the cart, if so increment the quantity.

2. Send user to basket page

3. Display cart contents

 

User deletes item -

1. Retrieve product from cart and decrease the quantity. If quantity is 1 remove the product

2. Send user to basket page

3. Display cart contents

 

User logs in -

1. Obtain guest session cart contents and transfer to database storage referenced by the user id

 

If you struggle with arrays or sessions then do some tutorials or reading up.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.