src/EventSubscriber/Order/CartActionSubscriber.php line 82

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Order;
  3. use App\Ecommerce\Model\Order;
  4. use App\Model\Customer;
  5. use Exception;
  6. use Factory\CartBundle\Classes\CartEvents;
  7. use Factory\CartBundle\Classes\DataMapper\CartDataMapper;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Factory\CartBundle\Event\CartEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Factory\CartBundle\Repository\CartRepository;
  12. use Pimcore\Bundle\EcommerceFrameworkBundle\EnvironmentInterface;
  13. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use App\Ecommerce\PriceSystem\PriceSystemRule\Processor as PriceSystemRuleProcessor;
  18. use App\Repository\OrderRepository;
  19. use Pimcore\Model\DataObject\PriceSystemRule;
  20. class CartActionSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var Security
  24.      */
  25.     protected $security;
  26.     /**
  27.      * @var RequestStack
  28.      */
  29.     protected $requestStack;
  30.     /**
  31.      * @var CartRepository
  32.      */
  33.     protected $cartRepository;
  34.     /**
  35.      * @var EnvironmentInterface
  36.      */
  37.     protected $environment;
  38.     /**
  39.      * @var OrderRepository
  40.      */
  41.     protected $orderRepository;
  42.     protected  $customer;
  43.     public static function getSubscribedEvents()
  44.     {
  45.         return [
  46.             CartEvents::CART_BEFORE_FETCH => [
  47.                 ['updateCurrency'11],
  48.                 ['fetchOrder'10]
  49.             ],
  50.             CartEvents::CART_BEFORE_UPDATE => [
  51.                 ['updateOrderItem'10]
  52.             ],
  53.             CartEvents::CART_BEFORE_REMOVE => [
  54.                 ['removeOrderItem'10]
  55.             ]
  56.         ];
  57.     }
  58.     public function __construct(
  59.         Security $security,
  60.         RequestStack $requestStack,
  61.         CartRepository $cartRepository,
  62.         OrderRepository $orderRepository,
  63.         EnvironmentInterface $environment
  64.     ) {
  65.         $this->security $security;
  66.         $this->requestStack $requestStack;
  67.         $this->cartRepository $cartRepository;
  68.         $this->environment $environment;
  69.         $this->orderRepository $orderRepository;
  70.     }
  71.     protected function getRequest(): Request
  72.     {
  73.         return $this->requestStack->getMasterRequest();
  74.     }
  75.     protected function getOrderFromReferer()
  76.     {
  77.         $httpReferer $this->getRequest()->server->get('HTTP_REFERER');
  78.         if (!preg_match('/\/order\-quote\/(\d+)/'$httpReferer$matches)) {
  79.             return null;
  80.         }
  81.         return Order::getById($matches[1]);
  82.     }
  83.     public function updateCurrency(CartEvent $event)
  84.     {
  85.         if (!$order $this->getOrderFromReferer()) {
  86.             return;
  87.         }
  88.         $this->updateEnvironmentWithAddress($order);
  89.         $this->updateEnvironmentWithShipping($order);
  90.         $currentCurrency $nextCurrency $order->getCurrency();
  91.         $address_id $this->getRequest()->get('address_id');
  92.         $nextCurrency $currentCurrency;
  93.         if ($address_id && $address_id != 'new') {
  94.             $nextCurrency $this->orderRepository->detectCurrency($order$address_id);
  95.         }
  96.         if ($currentCurrency == $nextCurrency) {
  97.             $event->stopPropagation();
  98.             $event->setResponse(new JsonResponse([
  99.                 'currency' => $nextCurrency,
  100.                 'data' => (new CartDataMapper($order))->all($this->getRequest())
  101.             ]));
  102.             return;
  103.         }
  104.         $this->orderRepository->convertOrderCurrency($order$nextCurrencyfalse);
  105.         $order->getPriceCalculator()->reset();
  106.         $event->stopPropagation();
  107.         $event->setResponse(new JsonResponse([
  108.             'currency' => $nextCurrency,
  109.             'data' => (new CartDataMapper($order))->all($this->getRequest())
  110.         ]));
  111.     }
  112.     protected function updateEnvironmentWithAddress($order)
  113.     {
  114.         $env Factory::getInstance()->getEnvironment();
  115.         if ($billingAddressId $this->getRequest()->cookies->get('__order_billing_address_id')) {
  116.             if ($billingAddressId == 'customer') {
  117.                 $billingAddress $this->orderRepository->createAddressFromOrder('customer'$order);
  118.             } else {
  119.                 $billingAddress $this->getCustomerAddress($billingAddressId);
  120.             }
  121.             $env->setBillingAddress($billingAddress);
  122.         }
  123.         if ($shippingAddressId $this->getRequest()->cookies->get('__order_shipping_address_id')) {
  124.             if ($shippingAddressId == 'delivery') {
  125.                 $shippingAddress $this->orderRepository->createAddressFromOrder('delivery'$order);
  126.             } else {
  127.                 $shippingAddress $this->getCustomerAddress($shippingAddressId);
  128.             }
  129.             $env->setShippingAddress($shippingAddress);
  130.         }
  131.     }
  132.     protected function updateEnvironmentWithShipping($order)
  133.     {
  134.         return;
  135.         if ($this->isPaymentStep()) {
  136.             return;
  137.         }
  138.         $env Factory::getInstance()->getEnvironment();
  139.         $index 'apply_factory_shipping__ord_'.$order->getId();
  140.         $env->setCustomItem($indexnull);
  141.     }
  142.     protected function isPaymentStep()
  143.     {
  144.         $httpReferer $this->getRequest()->server->get('HTTP_REFERER');
  145.         return preg_match('/\/order\-quote\/(\d+)\/payment/'$httpReferer$matches);
  146.     }
  147.     protected function getCurrentCustomer(): Customer
  148.     {
  149.         if (!$this->customer) {
  150.             $env Factory::getInstance()->getEnvironment();
  151.             $this->customer Customer::getById($env->getCurrentUserId());
  152.         }
  153.         return $this->customer;
  154.     }
  155.     protected function getCustomerAddress(string $addressId)
  156.     {
  157.         $customer $this->getCurrentCustomer();
  158.         return $customer->getAddress($addressId);
  159.     }
  160.     public function fetchOrder(CartEvent $event)
  161.     {
  162.         if (!$order $this->getOrderFromReferer()) {
  163.             return;
  164.         }
  165.         $event->stopPropagation();
  166.         $event->setResponse(new JsonResponse([
  167.             'data' => (new CartDataMapper($order))->all($this->getRequest())
  168.         ]));
  169.     }
  170.     public function updateOrderItem(CartEvent $event)
  171.     {
  172.         if (!$order $this->getOrderFromReferer()) {
  173.             return;
  174.         }
  175.         $quantities $this->getRequest()->get('quantities');
  176.         foreach ($order->getItems() as $orderItem) {
  177.             if (!isset($quantities[$orderItem->getProductNumber()])) {
  178.                 continue;
  179.             }
  180.             $quantity intVal($quantities[$orderItem->getProductNumber()]);
  181.             if ($quantity == 0) {
  182.                 $orderItem->delete();
  183.                 continue;
  184.             }
  185.             $orderItem->setAmount($quantity);
  186.             $orderItem->save();
  187.         }
  188.         $event->stopPropagation();
  189.         $event->setResponse(new JsonResponse([
  190.             'data' => (new CartDataMapper(Order::getById($order->getId(), true)))->all($this->getRequest())
  191.         ]));
  192.     }
  193.     public function removeOrderItem(CartEvent $event)
  194.     {
  195.         if (!$order $this->getOrderFromReferer()) {
  196.             return;
  197.         }
  198.         throw new \Exception('Not implemented. Instead of this action, use update with quantity 0 (zero)!');
  199.     }
  200. }