custom/plugins/HochwarthCheckoutCheckbox/src/Core/Checkout/CheckoutOrderPlacedSubscriber.php line 132

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace HochwarthCheckoutCheckbox\Core\Checkout;
  3.     use HochwarthCheckoutCheckbox\Core\Content\HochwarthCheckbox\HochwarthCheckboxEntity;
  4.     use HochwarthCheckoutCheckbox\Core\Content\HochwarthCheckbox\SalesChannel\AbstractHochwarthCheckboxRoute;
  5.     use HochwarthCheckoutCheckbox\Core\Content\HochwarthCheckbox\SalesChannel\HochwarthCheckboxRoute;
  6.     use HochwarthCheckoutCheckbox\HochwarthCheckoutCheckbox;
  7.     use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  8.     use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  9.     use Shopware\Core\Checkout\CheckoutRuleScope;
  10.     use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
  11.     use Shopware\Core\Content\Rule\RuleEntity;
  12.     use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13.     use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14.     use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  15.     use Shopware\Core\System\SalesChannel\Context\SalesChannelContextRestorer;
  16.     use Shopware\Core\System\SystemConfig\SystemConfigService;
  17.     use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18.     use Symfony\Component\HttpFoundation\RequestStack;
  19.     use Symfony\Contracts\EventDispatcher\Event;
  20.     class CheckoutOrderPlacedSubscriber implements EventSubscriberInterface
  21.     {
  22.         /**
  23.          * @var EntityRepositoryInterface
  24.          */
  25.         private $customerRepository;
  26.         /**
  27.          * @var RequestStack
  28.          */
  29.         private $requestStack;
  30.         /**
  31.          * @var SystemConfigService
  32.          */
  33.         private $systemConfigService;
  34.         /**
  35.          * @var EntityRepositoryInterface
  36.          */
  37.         private $orderRepository;
  38.         private HochwarthCheckboxRoute $checkboxRoute;
  39.         private EntityRepositoryInterface $ruleRepository;
  40.         private SalesChannelContextRestorer $salesChannelContextRestorer;
  41.         public function __construct(
  42.             SystemConfigService       $systemConfigService,
  43.             EntityRepositoryInterface $customerRepository,
  44.             EntityRepositoryInterface $orderRepository,
  45.             RequestStack              $requestStack,
  46.             AbstractHochwarthCheckboxRoute $checkboxRoute,
  47.             EntityRepositoryInterface $ruleRepository,
  48.             SalesChannelContextRestorer $salesChannelContextRestorer
  49.         ) {
  50.             $this->systemConfigService $systemConfigService;
  51.             $this->customerRepository $customerRepository;
  52.             $this->orderRepository $orderRepository;
  53.             $this->requestStack $requestStack;
  54.             $this->checkboxRoute $checkboxRoute;
  55.             $this->ruleRepository $ruleRepository;
  56.             $this->salesChannelContextRestorer $salesChannelContextRestorer;
  57.         }
  58.         public static function getSubscribedEvents(): array
  59.         {
  60.             return [
  61.                 CartConvertedEvent::class => 'onCartConverted',
  62.                 CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlaced',
  63.                 'state_enter.order.state.completed' => 'onEnterOrderStateCompleted'
  64.             ];
  65.         }
  66.         public function onCartConverted(CartConvertedEvent $event): void
  67.         {
  68.             if ($this->requestStack->getCurrentRequest()->request->get('hochwarth-checkout-checkbox') === null) {
  69.                 return;
  70.             }
  71.             $checkboxValue $this->requestStack->getCurrentRequest()->request->get('hochwarth-checkout-checkbox');
  72.             $data $event->getConvertedCart();
  73.             if (!is_array($checkboxValue)) {
  74.                 $saveCheckboxValueTo $this->systemConfigService->get('HochwarthCheckoutCheckbox.config.checkboxSave');
  75.                 if ($saveCheckboxValueTo === 'order') {
  76.                     $data['customFields'][HochwarthCheckoutCheckbox::CUSTOMER_MARKETING_ALLOWED] = (bool)$checkboxValue;
  77.                 } elseif ($saveCheckboxValueTo === 'customer') {
  78.                     $this->customerRepository->update([['id' => $data['orderCustomer']['customerId'], 'customFields' => [HochwarthCheckoutCheckbox::CUSTOMER_MARKETING_ALLOWED => (bool)$checkboxValue]]], $event->getContext());
  79.                 }
  80.                 $event->setConvertedCart($data);
  81.                 return;
  82.             }
  83.             $checkboxes $this->checkboxRoute->load($this->requestStack->getMasterRequest(), $event->getSalesChannelContext(), new Criteria())->getCheckboxes();
  84.             $saveCustomer = [];
  85.             $saveOrder = [];
  86.             /** @var HochwarthCheckboxEntity $checkbox */
  87.             foreach ($checkboxes as $checkbox) {
  88.                 switch ($checkbox->getStorage()) {
  89.                     case 'customer':
  90.                         $saveCustomer[] = [
  91.                             'id' => $checkbox->getId(),
  92.                             'label' => $checkbox->getAdminLabel() ?? 'Marketing checkbox',
  93.                             'value' => (bool)($checkboxValue[$checkbox->getId()] ?? false)
  94.                         ];
  95.                         break;
  96.                     case 'order':
  97.                         $saveOrder[] = [
  98.                             'id' => $checkbox->getId(),
  99.                             'label' => $checkbox->getAdminLabel() ?? 'Marketing checkbox',
  100.                             'value' => (bool)($checkboxValue[$checkbox->getId()] ?? false)
  101.                         ];
  102.                         break;
  103.                 }
  104.             }
  105.             if (\count($saveOrder)) {
  106.                 $data['customFields'][HochwarthCheckoutCheckbox::CUSTOMER_MARKETING_ALLOWED_MULTI] = $saveOrder;
  107.             }
  108.             if (\count($saveCustomer)) {
  109.                 $this->customerRepository->update([['id' => $data['orderCustomer']['customerId'], 'customFields' => [HochwarthCheckoutCheckbox::CUSTOMER_MARKETING_ALLOWED_MULTI => $saveCustomer]]], $event->getContext());
  110.             }
  111.             $event->setConvertedCart($data);
  112.         }
  113.         public function onCheckoutOrderPlaced(CheckoutOrderPlacedEvent $event): void
  114.         {
  115.             $this->setRules($event);
  116.         }
  117.         public function onEnterOrderStateCompleted(OrderStateMachineStateChangeEvent $event): void
  118.         {
  119.             $this->setRules($event);
  120.         }
  121.         /**
  122.          * @param CheckoutOrderPlacedEvent|OrderStateMachineStateChangeEvent $event
  123.          */
  124.         private function setRules(Event $event): void
  125.         {
  126.             $order $event->getOrder();
  127.             $criteria = new Criteria();
  128.             $criteria->addFilter(new EqualsFilter('conditions.type''hochwarthCheckoutCheckbox'));
  129.             $context $event->getContext();
  130.             $salesChannelContext $this->salesChannelContextRestorer->restoreByOrder($order->getId(), $context);
  131.             $rules $this->ruleRepository->search($criteria$context);
  132.             $ruleIds $context->getRuleIds();
  133.             /** @var RuleEntity $rule */
  134.             foreach ($rules as $rule) {
  135.                 if ($rule->getPayload()->match(new OrderRuleScope($salesChannelContext$order))) {
  136.                     $ruleIds[] = $rule->getId();
  137.                 }
  138.             }
  139.             $context->setRuleIds($ruleIds);
  140.         }
  141.     }