May 12, 2016
0
Level 12
Shopping cart and session id that change after login
I have a shopping cart where I save the session id, the item's id, qty, price and tax. It is work well but Laravel when I login Laravel change my session id and I have to add to my shopping cart all the items again. I am using Session::getId(); to get the user's session and then I store it in the database with the rest of data. What should I do to sort it out?
This is how I add and item to my cart:
public function store(Request $request)
{
$data = $request->all();
$numpro=count($data['product_id']);
$session = \Session::getId();
for ($i=0;$i<$numpro;$i++) {
if ($data['qty'][$i]>0) {
$product= Product::find($data['product_id'][$i]);
$cartdata = Cart::where('session', $session)->where('product_id', $data['product_id'][$i])->first();
if ($cartdata== null) {
$cart = new Cart([
'session' => $session,
'product_id' => $data['product_id'][$i],
'qty' => $data['qty'][$i],
'price' => $product['price'],
'tax' => $product['tax']
]);
$cart->save();
} else {
$cartdata->qty= $cartdata->qty+$data['qty'][$i];
$cartdata->save();
}
}
}
Please or to participate in this conversation.