Closed bobitza closed 2 years ago
I use like in documentation the addItem function to add a product in cart $item = Cart::addItem($product, 1, ['Size' => 'L']); But when i call second time ( i use ajax to call that controller) i got error:
$item = Cart::addItem($product, 1, ['Size' => 'L']);
"message": "Array to string conversion", "exception": "ErrorException", "file": "D:\\www\\iqshop\\vendor\\conedevelopment\\bazar\\src\\Concerns\\InteractsWithItems.php", "line": 313,
This is my action controller to add product to cart:
public function addToCart(Request $request) { //-- //-- $validator = Validator::make($request->all(), [ 'prodId' => 'required|exists:bazar_products,id|max:255', 'variant' => 'string', 'action' => 'required', ]); if ($validator->fails()) { return response()->json(['status' => 'ERROR', 'errors' => $validator->errors()]); } //-- $prodId = $request->input('prodId'); if((string)$prodId != '') { if((string)$request->input('action') == 'add') { //--Variant $variant = ''; $arrVariant = []; //-- $prodId, $variantId //--Product select from db $product = Product::proxy() ->newQuery() ->with(['variants']) ->find((int)$prodId) ->first(); if(is_null($product)) { return response()->json(['status' => 'ERROR', 'message' => 'Invalid product selected !']); } $cart = Cart::first(); //--Add item to cart $variant = (string) $request->input('variant'); if((string)$variant != '') { $varProduct = $product->toVariant(['Size' => (string) $variant]); if(!is_null($varProduct)) $product = $varProduct; $arrVariant = ['Size' => (string)$variant]; $item = Cart::addItem($product, 1, (array) $arrVariant); } else { $item = Cart::addItem($product, 1); } //end if if(is_null($item)) { return response()->json(['status' => 'ERROR', 'message' => 'Invalid add to cart prod selected !']); } //-- return response()->json(['status' => 'OK', 'message' => 'Produs adăugată cu success.', 'action' => 'add', 'title' => 'Success']); }else { return response()->json(['status' => 'ERROR', 'message' => 'Invalid action.']); } } }
If i don't specific some Size isn't work
Description:
I use like in documentation the addItem function to add a product in cart
$item = Cart::addItem($product, 1, ['Size' => 'L']);
But when i call second time ( i use ajax to call that controller) i got error:This is my action controller to add product to cart: