File: D:/HostingSpaces/SBogers10/vangogh.komma.pro/app/Komma/Shop/Cart/ShoppingCartController.php
<?php
namespace App\Komma\Shop\Cart;
use App\Komma\Base\Controller;
use App\Komma\Globalization\RegionInfo;
use App\Komma\Pages\PageService;
use App\Komma\Shop\Discounts\DiscountServiceInterface;
use Illuminate\Contracts\View\View;
/**
* Represents a shopping cart
*
* Class ShoppingCartService
* @package App\Komma\Shop\Cart
*/
class ShoppingCartController extends Controller
{
/**
* @var DiscountServiceInterface $discountService
*/
private $discountService;
public function __construct(PageService $pageService)
{
// $this->shoppingCartService = app(ShoppingCartService::class); //Don't do this. The shopping cart service restores itself using session. But the session is not yet initialized in constructors.
/** @var ShoppingCartServiceInterface $shoppingCartService */
$this->discountService = app(DiscountServiceInterface::class);
parent::__construct();
}
/**
* Get the shopping cart index
*
* @return View
*/
public function index() : View
{
/** @var ShoppingCartServiceInterface $shoppingCartService */
$shoppingCartService = app(ShoppingCartServiceInterface::class);
$items = $shoppingCartService->getItems();
$subTotal = $shoppingCartService->getTotal(false);
$itemCount = $shoppingCartService->getItemsCount();
$subTotalWithDiscounts = $shoppingCartService->getTotal(true);
$regionInfo = new RegionInfo('NL');
$languageMenu = [];
foreach ($this->site->languages as $language)
{
$languageMenu[$language->iso_2] = '/'.$language->iso_2.'/'.__('site/routes.shoppingcart', [], $language->iso_2);
}
$view = view('shop.pages.shoppingCart', [
'items' => $items,
'subTotal' => $subTotal,
'itemCount' => $itemCount,
'links' => $this->links,
'subTotalWithDiscounts' => $subTotalWithDiscounts,
'regionInfo' => $regionInfo,
'languageMenu' => $languageMenu,
]);
return $view;
}
/**
* Add a product to the cart
*
* @param AddRequest $request
* @return View
* @throws \Exception
*/
public function addProductToShoppingCart(AddRequest $request) : String
{
/** @var ShoppingCartServiceInterface $shoppingCartService */
$shoppingCartService = app(ShoppingCartServiceInterface::class);
$type = $request->get('itemType');
$productableId = $request->get('productableId');
$quantity = $request->get('quantity');
$productable = $type::where('id', $productableId)->with('translations')->first();
$shoppingCartService->addProductable($productable, $quantity);
return route('shoppingcart');
}
/**
* Add a product to the cart
*
* @param RemoveRequest $request
* @return View
*/
public function removeItemFromShoppingCart(RemoveRequest $request) : String
{
$type = $request->get('itemType');
$productableId = $request->get('productableId');
if($request->has('productableId')) {
$shoppingCartService = app(ShoppingCartServiceInterface::class);
$productable = $type::where('id', $productableId)->first();
$shoppingCartService->deleteItem($productable);
}
return route('shoppingcart');
}
/**
* Add a product to the cart
*
* @param UpdateAmountRequest $request
* @return View
*/
public function setItemAmountInShoppingCart(UpdateAmountRequest $request) : String
{
if($request->has('itemId')) {
$shoppingCartService = app(ShoppingCartServiceInterface::class);
$shoppingCartService->setItemAmount($request->get('itemId'), $request->get('amount'));
}
return route('shoppingcart');
}
}