55 lines
1.5 KiB
PHP
Executable File
55 lines
1.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Recettes\Controller;
|
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
use Thelia\Controller\Front\BaseFrontController;
|
|
use Thelia\Core\Event\TheliaEvents;
|
|
use Thelia\Core\HttpFoundation\Request;
|
|
use Thelia\Core\Event\Cart\CartEvent;
|
|
use Thelia\Model\ProductSaleElementsQuery;
|
|
use Thelia\Tools\URL;
|
|
|
|
|
|
/**
|
|
* Class FrontController
|
|
* @package Recettes\Controller
|
|
*/
|
|
class FrontController extends BaseFrontController
|
|
{
|
|
|
|
public function addToCart(Request $request)
|
|
{
|
|
$parameters = $request->request;
|
|
$dispatcher = $this->getDispatcher();
|
|
$produitsHorsStock = false;
|
|
|
|
foreach ($parameters as $key=>$value) {
|
|
$string = explode("-", $key);
|
|
$pseId = $string[1];;
|
|
$qty = $string[3];
|
|
|
|
$productId = ProductSaleElementsQuery::create()->findOneById($pseId)->getProductId();
|
|
$stock = ProductSaleElementsQuery::create()->findOneById($pseId)->getQuantity();
|
|
|
|
if ($stock > 0) {
|
|
$eventAjout = new CartEvent($request->getSession()->getSessionCart($dispatcher));
|
|
$eventAjout
|
|
->setQuantity($qty)
|
|
->setProduct($productId)
|
|
->setProductSaleElementsId($pseId);
|
|
|
|
$dispatcher->dispatch(TheliaEvents::CART_ADDITEM, $eventAjout);
|
|
}
|
|
else {
|
|
$produitsHorsStock = true;
|
|
}
|
|
}
|
|
|
|
if (! $produitsHorsStock)
|
|
return new RedirectResponse(URL::getInstance()->absoluteUrl('/cart'));
|
|
|
|
}
|
|
|
|
}
|