126 lines
3.9 KiB
PHP
126 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Product;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Collection;
|
|
use Gloudemans\Shoppingcart\Facades\Cart;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class BasketController extends Controller
|
|
{
|
|
/**
|
|
* ajout de produit au panier
|
|
*/
|
|
public function addToCart(Request $request)
|
|
{
|
|
$instance = $request->instance ?? "panier";
|
|
$product = Product::find($request->id);
|
|
Cart::instance($instance);
|
|
Cart::add($product, $request->qty);
|
|
return $this->getCart($instance);
|
|
}
|
|
|
|
/**
|
|
* ajout de plusieurs produits au panier
|
|
*/
|
|
public function addToCartLot(Request $request)
|
|
{
|
|
$instance = $request->instance ?? "panier";
|
|
Cart::instance($instance);
|
|
$lot = $request->items;
|
|
foreach($lot as $item) {
|
|
$product = Product::find($item["cartItem"]["id"]);
|
|
Cart::add($product, $item["cartItem"]["qty"]);
|
|
}
|
|
return $this->getCart($instance);
|
|
}
|
|
|
|
/**
|
|
* suppression de produit du panier
|
|
*/
|
|
public function removeFromCart(Request $request)
|
|
{
|
|
$instance = $request->instance ?? "panier";
|
|
$rowId = $request->rowId;
|
|
Cart::instance($instance);
|
|
Cart::remove($rowId);
|
|
return $this->getCart($instance);
|
|
}
|
|
|
|
/**
|
|
* vidage de panier
|
|
*/
|
|
public function emptyCart(Request $request) {
|
|
$instance = $request->instance ?? "panier";
|
|
Cart::instance($instance);
|
|
Cart::destroy();
|
|
return $this->getCart($instance);
|
|
}
|
|
|
|
/**
|
|
* récupération du panier
|
|
*/
|
|
public function getCartRequest(Request $request)
|
|
{
|
|
$instance = $request->instance ?? "panier";
|
|
Cart::instance($instance);
|
|
return $this->getCart($instance);
|
|
}
|
|
|
|
|
|
public function getCart($instance = "panier")
|
|
{
|
|
Cart::instance($instance);
|
|
$dbpanier = new Collection();
|
|
foreach (Cart::content() as $row) {
|
|
$product = $row->model;
|
|
//$product->additionnalProductInfo = $product->additionnalProductInfo($product->product_type);
|
|
$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(),
|
|
'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
|
|
);
|
|
|
|
$cartInfosNoPrice = array(
|
|
"totalTTC" => "-.--",
|
|
'totalTVA' => "-.--", //total des taxes
|
|
'totalHT' => "-.--", //total sans les taxes
|
|
'totalweight' => Cart::weight(), //poids total des items
|
|
'totalHTbefore' => "-.--", //total avant taxes et remises
|
|
'totaldiscount' => "-.--", //total des remises
|
|
'countItems' => Cart::count(), //nb de produits dans le cart
|
|
);
|
|
|
|
$cartReturn = array($dbpanier,$cartInfosNoPrice);
|
|
if (config('bosettings.show_prices_authenticated') == "authenticated") {
|
|
{
|
|
if (Auth::guard('customer')->check()) {
|
|
$cartReturn = array($dbpanier,$cartInfos);
|
|
}
|
|
}
|
|
}
|
|
if (config('bosettings.show_prices_guest') == "guest") {
|
|
$cartReturn = array($dbpanier,$cartInfos);
|
|
}
|
|
|
|
return $cartReturn;
|
|
}
|
|
}
|