conedevelopment / bazar

Bazar is an e-commerce package for Laravel applications.
https://root.conedevelopment.com
MIT License
421 stars 56 forks source link

Bug Cart::addItem #187

Closed bobitza closed 2 years ago

bobitza commented 2 years ago

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:

  "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.']);
            }
        }
    }
bobitza commented 2 years ago

If i don't specific some Size isn't work