2020-11-02 14:47:11 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
2021-03-24 17:58:27 +01:00
|
|
|
use App\Models\Product;
|
2020-11-02 14:47:11 +01:00
|
|
|
use Illuminate\Http\Request;
|
2021-03-25 15:48:07 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2021-03-24 17:58:27 +01:00
|
|
|
use Gloudemans\Shoppingcart\Facades\Cart;
|
2020-11-02 14:47:11 +01:00
|
|
|
|
|
|
|
|
class BasketController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function index(Request $request)
|
|
|
|
|
{
|
|
|
|
|
return view('basket');
|
|
|
|
|
}
|
2021-03-24 17:58:27 +01:00
|
|
|
|
|
|
|
|
public function addToCart(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$product = Product::find($request->id);
|
|
|
|
|
Cart::add($product, $request->qty);
|
|
|
|
|
|
2021-03-25 15:48:07 +01:00
|
|
|
return $this->getCart();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function removeFromCart(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$rowId = $request->rowId;
|
|
|
|
|
Cart::remove($rowId);
|
|
|
|
|
return $this->getCart();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function emptyCart() {
|
|
|
|
|
Cart::destroy();
|
|
|
|
|
return $this->getCart();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCart() {
|
2021-03-24 17:58:27 +01:00
|
|
|
|
2021-03-25 15:48:07 +01:00
|
|
|
$dbpanier = new Collection();
|
2021-03-24 17:58:27 +01:00
|
|
|
foreach (Cart::content() as $row) {
|
|
|
|
|
$product = $row->model;
|
2021-04-08 15:49:20 +02:00
|
|
|
//$product->additionnalProductInfo = $product->additionnalProductInfo($product->product_type);
|
|
|
|
|
$product->calculatedPrices = $product->getCalcPrices();
|
|
|
|
|
|
|
|
|
|
$row->setTaxRate($product->tax->rate);
|
|
|
|
|
$row->setDiscountRate($product->discountRate);
|
2021-03-25 15:48:07 +01:00
|
|
|
|
2021-03-24 17:58:27 +01:00
|
|
|
$cartextendedItem = array(
|
|
|
|
|
'cartItem' => $row,
|
|
|
|
|
'product' => $product
|
|
|
|
|
);
|
|
|
|
|
$dbpanier->add($cartextendedItem);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 15:48:07 +01:00
|
|
|
$cartInfos = array(
|
|
|
|
|
"totalTTC" => Cart::total(),
|
|
|
|
|
'totalTVA' => Cart::tax(), //total des taxes
|
|
|
|
|
'totalHT' => Cart::subtotal(), //total sans les taxes
|
|
|
|
|
'totalweight' => Cart::weight(), //poids total des items
|
|
|
|
|
'totalHTbefore' => Cart::priceTotal(), //total avant taxes et remises
|
|
|
|
|
'totaldiscount' => Cart::discount(), //total des remises
|
|
|
|
|
'countItems' => Cart::count(), //nb de produits dans le cart
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$cartReturn = array($dbpanier,$cartInfos);
|
|
|
|
|
|
|
|
|
|
return $cartReturn;
|
2021-03-24 17:58:27 +01:00
|
|
|
}
|
2020-11-02 14:47:11 +01:00
|
|
|
}
|