custom/plugins/TonurGreetingCard/src/Subscriber/GreetingCardSubscriber.php line 67

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Tonur\GreetingCard\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  4. use Shopware\Core\Content\Product\SalesChannel\Detail\AbstractProductDetailRoute;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  8. use Shopware\Storefront\Event\StorefrontRenderEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Tonur\GreetingCard\Service\GreetingCardServiceInterface;
  13. use Tonur\GreetingCard\Util\PluginConfigUtil;
  14. class GreetingCardSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var RequestStack
  18.      */
  19.     private $requestStack;
  20.     /**
  21.      * @var GreetingCardServiceInterface
  22.      */
  23.     private $greetingCardService;
  24.     /**
  25.      * @var AbstractProductDetailRoute
  26.      */
  27.     private $productRoute;
  28.     /**
  29.      * @var EntityRepositoryInterface
  30.      */
  31.     private $orderRepository;
  32.     private PluginConfigUtil $pluginConfig;
  33.     public function __construct(
  34.         RequestStack $requestStack,
  35.         GreetingCardServiceInterface $greetingCardService,
  36.         AbstractProductDetailRoute $productRoute,
  37.         EntityRepositoryInterface $orderRepository,
  38.         PluginConfigUtil $pluginConfig
  39.     )
  40.     {
  41.         $this->requestStack $requestStack;
  42.         $this->greetingCardService $greetingCardService;
  43.         $this->productRoute $productRoute;
  44.         $this->orderRepository $orderRepository;
  45.         $this->pluginConfig $pluginConfig;
  46.     }
  47.     public static function getSubscribedEvents()
  48.     {
  49.         return [
  50.             StorefrontRenderEvent::class => 'onStorefrontRender',
  51.             CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlaced',
  52.         ];
  53.     }
  54.     /**
  55.      * @param StorefrontRenderEvent $event
  56.      * @throws \Shopware\Core\Content\Product\Exception\ProductNotFoundException
  57.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  58.      * @codeCoverageIgnore
  59.      */
  60.     public function onStorefrontRender(StorefrontRenderEvent $event): void
  61.     {
  62.         if (!$this->pluginConfig->isPluginActive($event->getSalesChannelContext())) {
  63.             return;
  64.         }
  65.         if (strpos($event->getView(), 'storefront/page/checkout/cart/index.html.twig') !== false ||
  66.             strpos($event->getView(), 'storefront/page/checkout/confirm/index.html.twig') !== false) {
  67.             $event->setParameter(
  68.                 'repertusGreetingCardCartHasGreetingCard',
  69.                 $this->greetingCardService->checkIfCartHasAlreadyCard($event->getSalesChannelContext())
  70.             );
  71.             $event->setParameter(
  72.                 'repertusGreetingCardCartHasProductWithCard',
  73.                 $this->greetingCardService->checkIfCartContainsProductWithCard($event->getSalesChannelContext())
  74.             );
  75.             $salesChannelContext $event->getSalesChannelContext();
  76.             $greetingCards $this->greetingCardService->getGreetingCardsForSalesChannel($salesChannelContext);
  77.             $products = [];
  78.             $criteria = (new Criteria())
  79.                 ->addAssociation('manufacturer.media')
  80.                 ->addAssociation('options.group')
  81.                 ->addAssociation('properties.group')
  82.                 ->addAssociation('mainCategories.category');
  83.             $criteria
  84.                 ->getAssociation('media')
  85.                 ->addSorting(new FieldSorting('position'));
  86.             foreach ($greetingCards as $greetingCard) {
  87.                 $result $this->productRoute->load($greetingCard->getProductId(), new Request(), $salesChannelContext$criteria);
  88.                 $products[$greetingCard->getId()] = $result->getProduct();
  89.                 $products[$greetingCard->getId()]->assign([
  90.                     'greetingCardId' => $greetingCard->getId(),
  91.                     'greetingCardTextMaxLength' => $greetingCard->getTextMaxLength()
  92.                 ]);
  93.             }
  94.             $event->setParameter('repertusGreetingCards'$products);
  95.             $event->setParameter('repertusGreetingCardContainerId'\Shopware\Core\Framework\Uuid\Uuid::randomHex());
  96.         }
  97.     }
  98.     public function onCheckoutOrderPlaced(CheckoutOrderPlacedEvent $event): void
  99.     {
  100.         if (!$this->pluginConfig->isPluginActive(null$event->getSalesChannelId())) {
  101.             return;
  102.         }
  103.         $request $this->requestStack->getCurrentRequest();
  104.         if ($request !== null) {
  105.             $noInvoice $request->request->get('repertusNoInvoice');
  106.             if ($noInvoice !== null) {
  107.                 $order $event->getOrder();
  108.                 $customFields $order->getCustomFields() ?? [];
  109.                 $customFields['tonur_no_invoice'] = filter_var($noInvoiceFILTER_VALIDATE_BOOLEAN);
  110.                 $this->orderRepository->upsert([
  111.                     [
  112.                         'id' => $order->getId(),
  113.                         'customFields' => $customFields
  114.                     ]
  115.                 ], $event->getContext());
  116.             }
  117.         }
  118.     }
  119. }