File: D:/HostingSpaces/SBogers10/topswtwmobile.komma.pro/app/controllers/CartController.php
<?php
use KommaApp\Shop\Checkout\CheckoutSession\CheckoutSession;
use KommaApp\Shop\Modules\Modules;
use KommaApp\Shop\Checkout\Shipping\ShippingCostsRepository;
class CartController extends BaseController
{
protected $ajaxCall = false;
protected $functions;
protected $cart;
function __construct(CheckoutSession $checkoutSession)
{
$this->cart = $checkoutSession->cart();
if (isset($_GET['ajax'])) $this->ajaxCall = true;
}
public function index()
{
//Open the shippingCostRepository
$shippingCostsRepository = new ShippingCostsRepository();
//load all the shippingCosts
$shipping_costs = $shippingCostsRepository->getEntities();
return View::make( viewPrefix() . 'checkout.cart')->With([
'cart' => $this->cart,
'shipping_costs' => $shipping_costs,
'bodyClasses' => 'cart',
]);
}
/**
* This method will add an item to the cart from an external controller eg. ReOrderController
*
* @param $productId
* @param int $addQuantity
*/
public function addProductByIdExternal($productId, $addQuantity = 1)
{
if(!$this->cart->addProductById($productId, $addQuantity)) return false;
return true;
}
protected function addProductById($productId = null)
{
if(!$productId) \App::abort(404, 'No product id in the request');
$addQuantity = 1;
if (isset($_GET['quantity'])) $addQuantity = $_GET['quantity'];
$this->cart->addProductById($productId, $addQuantity);
if ($this->ajaxCall) return $this->cart->totalQuantity($productId);
return Redirect::route(\Route::Current()->getAction()['as']);
}
protected function subtractProductById($productId)
{
$this->cart->subtractProductById($productId);
if ($this->ajaxCall) return $this->cart->totalQuantity($productId);
return Redirect::route(\Route::Current()->getAction()['as']);
}
protected function removeProductById($productId)
{
$this->cart->removeProductById($productId);
if ($this->ajaxCall) return $this->cart->totalQuantity($productId);
return Redirect::route(\Route::Current()->getAction()['as']);
}
protected function setQuantityProductById($productId, $quantity)
{
$this->cart->setQuantityProductById($productId, $quantity);
if ($this->ajaxCall) return $this->cart->totalQuantity($productId);
return Redirect::route(\Route::Current()->getAction()['as']);
}
protected function checkCouponCode()
{
return json_encode($this->cart->getCouponCodeBatchId(\Request::get('code')));
}
public function getCartAsJson()
{
return \Response::make($this->cart->asJson(), 200)->header('Content-Type', 'application/json');
}
}