<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace App\Controller;
use App\Entity\FreeOption\FreeOption;
use FOS\RestBundle\View\View;
use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface;
use Sylius\Component\Order\CartActions;
use Sylius\Component\Order\Model\OrderItemInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Sylius\Bundle\OrderBundle\Controller\OrderItemController as BaseOrderItemController;
class OrderItemController extends BaseOrderItemController
{
public function addAction(Request $request): Response
{
$cart = $this->getCurrentCart();
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
$this->isGrantedOr403($configuration, CartActions::ADD);
/** @var OrderItemInterface $orderItem */
$orderItem = $this->newResourceFactory->create($configuration, $this->factory);
$this->getQuantityModifier()->modify($orderItem, 1);
$form = $this->getFormFactory()->create(
$configuration->getFormType(),
$this->createAddToCartCommand($cart, $orderItem),
$configuration->getFormOptions()
);
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
/** @var AddToCartCommandInterface $addToCartCommand */
$addToCartCommand = $form->getData();
$request->getSession()->set('slug_product_taxon_referer', $orderItem->getProduct()->getSlug());
$errors = $this->getCartItemErrors($addToCartCommand->getCartItem());
if (0 < count($errors)) {
$form = $this->getAddToCartFormWithErrors($errors, $form);
return $this->handleBadAjaxRequestView($configuration, $form);
}
$event = $this->eventDispatcher->dispatchPreEvent(CartActions::ADD, $configuration, $orderItem);
if ($event->isStopped() && !$configuration->isHtmlRequest()) {
throw new HttpException($event->getErrorCode(), $event->getMessage());
}
if ($event->isStopped()) {
$this->flashHelper->addFlashFromEvent($configuration, $event);
return $this->redirectHandler->redirectToIndex($configuration, $orderItem);
}
$freeOptions = [];
$freeOptionValueRepository = $this->get('app.repository.free_option_value');
foreach ($request->request->all() as $key => $value) {
if (str_starts_with($key, 'free_option_quantity_')) {
$optionId = str_replace('free_option_quantity_', '', $key);
$optionValueId = $request->request->get('option_'.$optionId);
if (null === $optionValueId) {
continue;
}
/** @var FreeOption $freeOption */
$freeOptionValue = $freeOptionValueRepository->find($optionValueId);
if ($freeOptionValue) {
$price = $freeOptionValue->getPrice();
$freeOptions[] = ['id' => $optionValueId, 'option_id' => $optionId,'quantity' => $value, 'price' => $price, 'ref' => $freeOptionValue->getRef(), 'title' => $freeOptionValue->getTranslation()->getTitle(), 'option_title' => $freeOptionValue->getFreeOption()->getTranslation()->getTitle()];
}
}
}
if (!empty($freeOptions)) {
$addToCartCommand->getCartItem()->setFreeOptions($freeOptions);
}
$this->getOrderModifier()->addToOrder($addToCartCommand->getCart(), $addToCartCommand->getCartItem());
$cartManager = $this->getCartManager();
$cartManager->persist($cart);
$cartManager->flush();
$resourceControllerEvent = $this->eventDispatcher->dispatchPostEvent(CartActions::ADD, $configuration, $orderItem);
if ($resourceControllerEvent->hasResponse()) {
return $resourceControllerEvent->getResponse();
}
$this->flashHelper->addSuccessFlash($configuration, CartActions::ADD, $orderItem);
if ($request->isXmlHttpRequest()) {
return $this->viewHandler->handle($configuration, View::create([], Response::HTTP_CREATED));
}
return $this->redirectHandler->redirectToResource($configuration, $orderItem);
}
if (!$configuration->isHtmlRequest()) {
return $this->handleBadAjaxRequestView($configuration, $form);
}
$view = View::create()
->setData([
'configuration' => $configuration,
$this->metadata->getName() => $orderItem,
'form' => $form->createView(),
])
->setTemplate($configuration->getTemplate(CartActions::ADD . '.html'))
;
return $this->viewHandler->handle($configuration, $view);
}
}