custom/plugins/NetiNextGoogleCustomerReviews/src/Subscriber/Checkout.php line 68

Open in your IDE?
  1. <?php
  2. namespace NetInventors\NetiNextGoogleCustomerReviews\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use NetInventors\NetiNextGoogleCustomerReviews\Service\PluginConfig;
  5. use NetInventors\NetiNextGoogleCustomerReviews\Struct\GoogleSurveyOptin;
  6. use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
  7. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
  8. use Shopware\Core\Content\Product\ProductEntity;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class Checkout implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var PluginConfig
  20.      */
  21.     private $pluginConfig;
  22.     /**
  23.      * @var Connection
  24.      */
  25.     private $db;
  26.     /**
  27.      * @var EntityRepositoryInterface
  28.      */
  29.     private $countryRepository;
  30.     /**
  31.      * @var EntityRepositoryInterface
  32.      */
  33.     private $productRepository;
  34.     /**
  35.      * Checkout constructor.
  36.      *
  37.      * @param PluginConfig              $pluginConfig
  38.      * @param Connection                $db
  39.      * @param EntityRepositoryInterface $countryRepository
  40.      * @param EntityRepositoryInterface $productRepository
  41.      */
  42.     public function __construct(
  43.         PluginConfig $pluginConfig,
  44.         Connection $db,
  45.         EntityRepositoryInterface $countryRepository,
  46.         EntityRepositoryInterface $productRepository
  47.     ) {
  48.         $this->pluginConfig      $pluginConfig;
  49.         $this->countryRepository $countryRepository;
  50.         $this->db                $db;
  51.         $this->productRepository $productRepository;
  52.     }
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             CheckoutFinishPageLoadedEvent::class => 'onFinishPageLoaded',
  57.         ];
  58.     }
  59.     public function onFinishPageLoaded(CheckoutFinishPageLoadedEvent $event): void
  60.     {
  61.         if (!$this->pluginConfig->isActive()) {
  62.             return;
  63.         }
  64.         $order           $event->getPage()->getOrder();
  65.         $deliverDate     = new \DateTime('now +' $this->pluginConfig->getDeliveryDate() . ' days');
  66.         $countryId       current($order->getDeliveries()->getShippingAddress()->getCountryIds());
  67.         $deliveryCountry $this->getCountryIsoById($countryId$event->getContext());
  68.         $struct = new GoogleSurveyOptin();
  69.         $struct->setMerchantId($this->pluginConfig->getMerchantId());
  70.         $struct->setOrderId($order->getId());
  71.         $struct->setEmail($order->getOrderCustomer()->getEmail());
  72.         $struct->setDeliveryCountry($deliveryCountry);
  73.         $struct->setEstimatedDeliveryDate($deliverDate);
  74.         $struct->setOptionalFields($this->pluginConfig->getOptionalFields());
  75.         if ($this->pluginConfig->isGtinActive()) {
  76.             $products $this->loadProducts($order->getLineItems(), $event->getContext());
  77.             $struct->setProducts($products);
  78.         }
  79.         $event->getPage()->addExtension('netiNextGoogleCustomerReviewsFinish'$struct);
  80.     }
  81.     /**
  82.      * @param OrderLineItemCollection $lineItems
  83.      * @param Context                 $context
  84.      *
  85.      * @return array
  86.      * @throws InconsistentCriteriaIdsException
  87.      */
  88.     protected function loadProducts(OrderLineItemCollection $lineItemsContext $context): array
  89.     {
  90.         $productIds = [];
  91.         foreach ($lineItems as $lineItem) {
  92.             if ('product' === $lineItem->getType()) {
  93.                 $productIds[] = $lineItem->getProductId();
  94.             }
  95.         }
  96.         $criteria = new Criteria($productIds);
  97.         $products $this->productRepository->search($criteria$context);
  98.         $result   = [];
  99.         /** @var ProductEntity $product */
  100.         foreach ($products as $product) {
  101.             if (!$product->getEan()) {
  102.                 continue;
  103.             }
  104.             $result[] = [
  105.                 'gtin' => $product->getEan(),
  106.             ];
  107.         }
  108.         return $result;
  109.     }
  110.     /**
  111.      * @param string  $countryId
  112.      * @param Context $context
  113.      *
  114.      * @return string|null
  115.      * @throws InconsistentCriteriaIdsException
  116.      */
  117.     protected function getCountryIsoById(string $countryIdContext $context): ?string
  118.     {
  119.         $criteria = new Criteria();
  120.         $criteria->addFilter(new EqualsFilter('id'$countryId));
  121.         $countries $this->countryRepository->search($criteria$context);
  122.         if ($countries->count() > 0) {
  123.             return $countries->first()->getIso();
  124.         }
  125.         return null;
  126.     }
  127. }