custom/static-plugins/K3nAgrilityConnector/src/Subscriber/OrderSubscriber.php line 69

Open in your IDE?
  1. <?php
  2. namespace K3n\K3nAgrilityConnector\Subscriber;
  3. use K3n\K3nAgrilityConnector\Services\AgrilityApiHandler;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  5. use Shopware\Core\Checkout\Order\OrderDefinition;
  6. use Shopware\Core\Checkout\Order\OrderEntity;
  7. use Shopware\Core\Checkout\Order\OrderEvents;
  8. use Shopware\Core\Content\Category\CategoryEntity;
  9. use Shopware\Core\Defaults;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\InsertCommand;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Write\WriteException;
  21. use Shopware\Core\Framework\Uuid\Uuid;
  22. use Shopware\Core\System\SystemConfig\SystemConfigService;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. class OrderSubscriber implements EventSubscriberInterface
  25. {
  26.     /**
  27.      * @var EntityRepositoryInterface
  28.      */
  29.     private $orderRepository;
  30.     /**
  31.      * @var EntityRepositoryInterface
  32.      */
  33.     private $categoryRepository;
  34.     /**
  35.      * @var AgrilityApiHandler
  36.      */
  37.     private $agrilityApiHandler;
  38.     /**
  39.      * @var SystemConfigService
  40.      */
  41.     private $systemConfigService;
  42.     public function __construct(
  43.         EntityRepositoryInterface $orderRepository,
  44.         EntityRepositoryInterface $categoryRepository,
  45.         AgrilityApiHandler $agrilityApiHandler,
  46.         SystemConfigService $systemConfigService
  47.     ) {
  48.         $this->orderRepository     $orderRepository;
  49.         $this->categoryRepository  $categoryRepository;
  50.         $this->agrilityApiHandler  $agrilityApiHandler;
  51.         $this->systemConfigService $systemConfigService;
  52.     }
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderWritten',
  57.         ];
  58.     }
  59.     public function onOrderWritten(EntityWrittenEvent $event)
  60.     {
  61.         $writeResults $event->getWriteResults();
  62.         foreach ($writeResults as $writeResult) {
  63.             $entityName $writeResult->getEntityName();
  64.             $operation  $writeResult->getOperation();
  65.             if ($entityName == OrderDefinition::ENTITY_NAME && $operation == EntityWriteResult::OPERATION_INSERT) {
  66.                 $orderId $writeResult->getPayload()['id'] ?? null;
  67.                 if ($orderId) {
  68.                     $context $event->getContext();
  69.                     $order   $this->getOrderById($orderId$context);
  70.                     if ($order) {
  71.                         if ($order->getVersionId() == Defaults::LIVE_VERSION) {
  72.                             $agrilityProduct $this->systemConfigService->get'K3nAgrilityConnector.config.agrilityProduct');
  73.                             $lineItems $order->getLineItems()->getElements();
  74.                             foreach ($lineItems as $lineItem) {
  75.                                 if($lineItem->getType() == LineItem::PRODUCT_LINE_ITEM_TYPE) {
  76.                                     $product $lineItem->getProduct();
  77.                                     if($agrilityProduct == $product->getId()) {
  78.                                         $customer $order->getOrderCustomer()->getCustomer();
  79.                                         $this->agrilityApiHandler->createUpdateFarmer($customer$context);
  80.                                         break;
  81.                                     }
  82.                                 }
  83.                             }
  84.                         }
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.     }
  90.     private function getOrderById(string $orderIdContext $context): ?OrderEntity
  91.     {
  92.         $criteria = new Criteria([$orderId]);
  93.         $criteria->addAssociation('lineItems');
  94.         $criteria->addAssociation('lineItems.product');
  95.         $criteria->addAssociation('orderCustomer.customer');
  96.         $criteria->addAssociation('orderCustomer.customer.defaultBillingAddress');
  97.         $criteria->addAssociation('orderCustomer.customer.defaultBillingAddress.company');
  98.         $criteria->addAssociation('orderCustomer.customer.defaultBillingAddress.country');
  99.         $criteria->setLimit(1);
  100.         return $this->orderRepository->search($criteria$context)->first();
  101.     }
  102.     private function getAgrilityCategory(Context $context): ?CategoryEntity
  103.     {
  104.         $criteria = new Criteria();
  105.         $criteria->addFilter(new OrFilter([
  106.             new EqualsFilter('name''agrility'),
  107.             new EqualsFilter('name''Agrility'),
  108.             new EqualsFilter('name''AGRILITY'),
  109.         ]));
  110.         $criteria->setLimit(1);
  111.         return $this->categoryRepository->search($criteria$context)->first();
  112.     }
  113. }