Hi i'm doing a ecommerce, and i have problem to store my session cart updated in my table CartItems, when user add product my controller put all information in my session "cart"
i created a product_session = category+productID+color+size, so i can difference between other products variations.
My problem is that after updated Session "cart" (it work good) i need update my table "CartItems" with all products of my session cart. I dont why my controller update only the last added product in cart session.
My controller CartController.php
public function add(Product $product, CartSession $CartSession,Request $request)
{
$id = $request->get('id');
$cart = \Session::get('cart'); // ricevo la var di sessione cart e la salvo su cart
foreach($cart as $producto){
$product_exist = $producto->id;
}
if(isset($product_exist)) {
if($product_exist == $product->id
&& $producto->color != $request->get('color')
|| $producto->size != $request->get('size') ) {
$product->quantity = $request->get('qty');
$product->size = $request->get('size');
$product->color = $request->get('color');
$product->category = $request->get('category');
$cart[$product->category.$product->id.$product->size.$product->color] = $product;
\Session::put('cart', $cart);
//return $cart;
}
if($product_exist == $product->id
&& $producto->color == $request->get('color')
&& $producto->size == $request->get('size') ) {
$product->quantity = $request->get('qty')+$producto->quantity;
$product->size = $request->get('size');
$product->color = $request->get('color');
$product->category = $request->get('category');
$cart[$product->category.$product->id.$product->size.$product->color] = $product;
\Session::put('cart', $cart);
} // fine if
}else {// fine if isserT, Se non ci sono prodotti già esistenti
$product->quantity = $request->get('qty');
$product->size = $request->get('size');
$product->color = $request->get('color');
$product->category = $request->get('category');
$cart[$product->category.$product->id.$product->size.$product->color] = $product;
\Session::put('cart', $cart);
}
//return $cart;
$subtotal = 0;
foreach($cart as $producto){
$subtotal += $producto->quantity * $producto->price;
}
$session_code = Session::getId();
//$CartSession = new CartSession();
$session_exist = CartSession::where('session_code', $session_code)->orderBy('id', 'desc')->first();
if (isset($session_exist)) {
$s = new CartSession;
$data = array(
'subtotal' => $subtotal,
);
$s->where('session_code', '=', $session_code)->update($data);
}else {
$CartSession = new CartSession();
$CartSession->session_code = $session_code;
$CartSession->subtotal = $subtotal;
$CartSession->save();
}
foreach($cart as $producto){
$this->saveCartItem($producto, $CartSession->id,$cart);
}
return redirect()->route('cart-show');
}
protected function saveCartItem($producto, $CartSession_id,$cart)
{
$session_code = Session::getId();
$get_items = CartItem::where('session_code', $session_code);
$deleted=$get_items->delete();
$get_id = CartSession::where('session_code', $session_code)->orderBy('id', 'desc')->first();
$CartItem = new CartItem();
$CartItem->price = $producto->price;
$CartItem->quantity = $producto->quantity;
$CartItem->size = $producto->size;
$CartItem->color = $producto->color;
$CartItem->category = $producto->category;
$CartItem->product_session = $producto->category.$producto->id.$producto->size.$producto->color;
$CartItem->product_id = $producto->id;
$CartItem->session_id = $get_id->id;
$CartItem->session_code = $session_code;
$CartItem->save();
}
the process that i'm doing on method saveCartItem is:
- Get all items products that have the same session and delete them
- Store the products of my session $cart ( these new data is updated with all products added, quantity changed, ecc )
Actually my controller delete products that have the same session and store only the last product added and NOT all products in my session cart.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire