json($basket->content()); } /** * Ajout d'un produit au panier * * @param \App\Models\Product $product * @return \Illuminate\Http\JsonResponse */ public function addToBasket(Product $product) { /** @var \Gloudemans\Shoppingcart\Cart */ $basket = app('basket'); // ! Use cart facade here because of lib bug // ! https://github.com/bumbummen99/LaravelShoppingcart/pull/181 // * Note Lib is is actually refactoring for laravel 9 php8 only // * https://github.com/bumbummen99/LaravelShoppingcart/pull/145 FacadesCart::add($product, 1, [ 'slug' => $product->slug, 'picture' => $product->picture, 'sell_unit' => $product->sell_unit, 'weight_with_unit' => WeightUnit::getDisplayableWeight($product->weight) ]); return \response()->json($basket->content()); } /** * Retrait d'un produit du panier (1 quantité d'1 type) * * @param \App\Models\Product $product * @return \Illuminate\Http\JsonResponse */ public function removeFromBasket(Product $product) { /** @var \Gloudemans\Shoppingcart\Cart */ $basket = app('basket'); /** @var \Gloudemans\Shoppingcart\CartItem|null */ $basketItem = $basket->content()->where('id', $product->id)->first(); /** @var string|null */ $rowId = \optional($basketItem)->rowId; /** @var integer|null */ $qty = \optional($basketItem)->qty; if ($rowId and !\is_null($qty)) { $basket->update($rowId, $qty - 1); } return \response()->json($basket->content()); } /** * Retrait d'un type produit du panier (toute les quantités d'1 type) * * @param \App\Models\Product $product * @return \Illuminate\Http\JsonResponse */ public function removeItemTypeFromBasket(Product $product) { /** @var \Gloudemans\Shoppingcart\Cart */ $basket = app('basket'); /** @var \Gloudemans\Shoppingcart\CartItem|null */ $basketItem = $basket->content()->where('id', $product->id)->first(); /** @var string|null */ $rowId = \optional($basketItem)->rowId; if ($rowId) { $basket->remove($rowId); } return \response()->json($basket->content()); } /** * Retrait d'un type produit du panier (toute les quantités d'1 type) * * @return \Illuminate\Http\JsonResponse */ public function emptyBasket() { /** @var \Gloudemans\Shoppingcart\Cart */ $basket = app('basket'); $basket->destroy(); /** @var \Gloudemans\Shoppingcart\Cart */ $basket = app('basket'); return \response()->json($basket->content()); } /** * return cart data array * * @param string $instance * @return array */ public function getCart(string $instance = "panier") { Cart::instance($instance); $dbpanier = new Collection(); foreach (Cart::content() as $row) { $product = $row->model; $product->calculatedPrices = $product->getCalcPrices(); $row->setTaxRate($product->tax->rate); $row->setDiscountRate($product->discountRate); $cartextendedItem = array( 'cartItem' => $row, 'product' => $product ); $dbpanier->add($cartextendedItem); } $cartInfos = array( "totalTTC" => Cart::total(), // Total des taxes. 'totalTVA' => Cart::tax(), // Total sans les taxes. 'totalHT' => Cart::subtotal(), // Poids total des items. 'totalweight' => Cart::weight(), // Total avant taxes et remises. 'totalHTbefore' => Cart::priceTotal(), // Total des remises. 'totaldiscount' => Cart::discount(), // Nb de produits dans le cart. 'countItems' => Cart::count(), ); $cartInfosNoPrice = array( "totalTTC" => "-.--", // Total des taxes. 'totalTVA' => "-.--", // Total sans les taxes. 'totalHT' => "-.--", // Poids total des items. 'totalweight' => Cart::weight(), // Total avant taxes et remises. 'totalHTbefore' => "-.--", // Total des remises. 'totaldiscount' => "-.--", // Nb de produits dans le cart. 'countItems' => Cart::count(), ); $cartReturn = array($dbpanier, $cartInfosNoPrice); /** @var \App\Models\GeneralConfig */ $config = Cache::get('GeneralConfigGlobal')->where('type', GeneralConfigType::global())->first(); // ? Don't show prices only for auth customers ? if (!$config->data['orders_display_prices_for_auth']['value']) { return $cartReturn; } // ? Customer is auth ? if (Auth::guard('frontend')->check()) { $cartReturn = array($dbpanier, $cartInfos); } return $cartReturn; } }