custom/plugins/TonurGiftWrap/src/Subscriber/GiftOptionSubscriber.php line 70

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Tonur\GiftOption\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  4. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  5. use Shopware\Core\Content\Product\SalesChannel\Detail\AbstractProductDetailRoute;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Shopware\Storefront\Event\StorefrontRenderEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Tonur\GiftOption\Service\GiftWrapService;
  17. use Tonur\GiftOption\TonurGiftWrap;
  18. class GiftOptionSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var RequestStack
  22.      */
  23.     private $requestStack;
  24.     /**
  25.      * @var GiftWrapService
  26.      */
  27.     private $giftWrapService;
  28.     /**
  29.      * @var AbstractProductDetailRoute
  30.      */
  31.     private $productRoute;
  32.     /**
  33.      * @var SystemConfigService
  34.      */
  35.     private $systemConfigService;
  36.     /**
  37.      * @var EntityRepositoryInterface
  38.      */
  39.     private $orderRepository;
  40.     public function __construct(
  41.         RequestStack $requestStack,
  42.         GiftWrapService $giftWrapService,
  43.         AbstractProductDetailRoute $productRoute,
  44.         SystemConfigService $systemConfigService,
  45.         EntityRepositoryInterface $orderRepository
  46.     )
  47.     {
  48.         $this->requestStack $requestStack;
  49.         $this->giftWrapService $giftWrapService;
  50.         $this->productRoute $productRoute;
  51.         $this->systemConfigService $systemConfigService;
  52.         $this->orderRepository $orderRepository;
  53.     }
  54.     public static function getSubscribedEvents()
  55.     {
  56.         return [
  57.             BeforeLineItemAddedEvent::class => 'onBeforeLineItemAdded',
  58.             StorefrontRenderEvent::class => 'onStorefrontRender',
  59.             CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlaced',
  60.         ];
  61.     }
  62.     public function onBeforeLineItemAdded(BeforeLineItemAddedEvent $event): void
  63.     {
  64.         $config $this->getPluginConfiguration($event->getSalesChannelContext());
  65.         if (!$config['pluginActive']) {
  66.             return;
  67.         }
  68.         $lineItem $event->getLineItem();
  69.         $lineItemId $lineItem->getId();
  70.         $currentRequest $this->requestStack->getCurrentRequest();
  71.         if ($currentRequest !== null && is_array($currentRequest->get('lineItems'))) {
  72.             foreach ($currentRequest->get('lineItems') as $key =>  $item) {
  73.                 if ($lineItemId === $key && isset($item['repertusShouldWrap'])) {
  74.                     if (!$this->giftWrapService->isLineItemPackable($lineItem$event->getSalesChannelContext())) {
  75.                         continue;
  76.                     }
  77.                     $repertusShouldWrap filter_var($item['repertusShouldWrap'], FILTER_VALIDATE_BOOLEAN);
  78.                     $lineItem->setPayloadValue('repertusShouldWrap'$repertusShouldWrap);
  79.                 }
  80.             }
  81.         }
  82.     }
  83.     /**
  84.      * @param StorefrontRenderEvent $event
  85.      * @throws \Shopware\Core\Content\Product\Exception\ProductNotFoundException
  86.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  87.      * @codeCoverageIgnore
  88.      */
  89.     public function onStorefrontRender(StorefrontRenderEvent $event): void
  90.     {
  91.         $config $this->getPluginConfiguration($event->getSalesChannelContext());
  92.         if (!$config['pluginActive']) {
  93.             return;
  94.         }
  95.         if (strpos($event->getView(), 'storefront/page/checkout/cart/index.html.twig') !== false ||
  96.             strpos($event->getView(), 'storefront/page/checkout/confirm/index.html.twig') !== false) {
  97.             $event->setParameter(
  98.                 'repertusGiftOptionCartIsWrapped',
  99.                 $this->giftWrapService->checkIfCartIsWrapped($event->getSalesChannelContext())
  100.             );
  101.             $event->setParameter(
  102.                 'repertusGiftOptionCartHasWrappedProduct',
  103.                 $this->giftWrapService->checkIfCartContainsWrappedProduct($event->getSalesChannelContext())
  104.             );
  105.             $salesChannelContext $event->getSalesChannelContext();
  106.             $giftWraps $this->giftWrapService->getGiftWrapsForSalesChannel($salesChannelContext);
  107.             $products = [];
  108.             $criteria = (new Criteria())
  109.                 ->addAssociation('manufacturer.media')
  110.                 ->addAssociation('options.group')
  111.                 ->addAssociation('properties.group')
  112.                 ->addAssociation('mainCategories.category');
  113.             $criteria
  114.                 ->getAssociation('media')
  115.                 ->addSorting(new FieldSorting('position'));
  116.             foreach ($giftWraps as $giftWrap) {
  117.                 $result $this->productRoute->load($giftWrap->getProductId(), new Request(), $salesChannelContext$criteria);
  118.                 $products[$giftWrap->getId()] = $result->getProduct();
  119.             }
  120.             $event->setParameter('repertusGiftWraps'$products);
  121.             $event->setParameter('repertusGiftWrapContainerId'Uuid::randomHex());
  122.         }
  123.     }
  124.     public function onCheckoutOrderPlaced(CheckoutOrderPlacedEvent $event): void
  125.     {
  126.         $config $this->getPluginConfiguration(null);
  127.         if (!$config['pluginActive']) {
  128.             return;
  129.         }
  130.         $request $this->requestStack->getCurrentRequest();
  131.         if ($request !== null) {
  132.             $noInvoice $request->request->get('repertusNoInvoice');
  133.             if ($noInvoice !== null) {
  134.                 $order $event->getOrder();
  135.                 $customFields $order->getCustomFields() ?? [];
  136.                 $customFields['tonur_no_invoice'] = filter_var($noInvoiceFILTER_VALIDATE_BOOLEAN);
  137.                 $this->orderRepository->upsert([
  138.                     [
  139.                         'id' => $order->getId(),
  140.                         'customFields' => $customFields
  141.                     ]
  142.                 ], $event->getContext());
  143.             }
  144.         }
  145.     }
  146.     protected function getPluginConfiguration(?SalesChannelContext $context): array
  147.     {
  148.         return $this->systemConfigService->get(TonurGiftWrap::BUNDLE_NAME '.config'$context $context->getSalesChannel()->getId() : null);
  149.     }
  150. }