custom/plugins/GrecoBarcode/src/Subscriber/OrderBarcodeSubscriber.php line 84

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace GrecoBarcode\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\System\SalesChannel\Event\SalesChannelContextTokenChangeEvent;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class OrderBarcodeSubscriber implements EventSubscriberInterface
  12. {
  13.     private EntityRepository $cartBarcodeRepository;
  14.     private SystemConfigService $systemConfigService;
  15.     public function __construct(
  16.         EntityRepository $cartBarcodeRepository,
  17.         SystemConfigService $systemConfigService
  18.     )
  19.     {
  20.         $this->cartBarcodeRepository $cartBarcodeRepository;
  21.         $this->systemConfigService $systemConfigService;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  26.         return [
  27.             CartConvertedEvent::class => 'onCartConverted',
  28.             SalesChannelContextTokenChangeEvent::class => 'onTokenChanged'
  29.         ];
  30.     }
  31.     public function onCartConverted(CartConvertedEvent $event): void
  32.     {
  33.         $configBarcodeActive $this->systemConfigService->get('GrecoBarcode.config.active');
  34.         if ($configBarcodeActive === false) {
  35.             return;
  36.         }
  37.         /**
  38.          * ["id", "orderNumber"]
  39.          */
  40.         $order $event->getConvertedCart();
  41.         $cart $event->getCart();
  42.         $configBarcodeProducts $this->systemConfigService->get('GrecoBarcode.config.products');
  43.         if (!$configBarcodeProducts || count($configBarcodeProducts) <= 0) {
  44.             $active true;
  45.         } else {
  46.             $active false;
  47.             foreach ($cart->getLineItems() as $item) {
  48.                 if (in_array($item->getId(), $configBarcodeProducts)) {
  49.                     $active true;
  50.                 }
  51.             }
  52.         }
  53.         if (!$active) {
  54.             return;
  55.         }
  56.         $criteria = new Criteria();
  57.         $criteria->addFilter(new EqualsFilter('cart_token'$cart->getToken()));
  58.         try {
  59.             $cartBarcode $this->cartBarcodeRepository->search($criteriaContext::createDefaultContext())->first();
  60.             if ($cartBarcode) {
  61.                 $cartBarcode $cartBarcode->getBarcode();
  62.             }
  63.         } catch (\Exception $ex) {
  64.             $cartBarcode null;
  65.         }
  66.         if (!$cartBarcode) {
  67.             return;
  68.         }
  69.         $order['customFields']['barcode'] = $cartBarcode;
  70.         $event->setConvertedCart($order);
  71.     }
  72.     public function onTokenChanged(SalesChannelContextTokenChangeEvent $event) {
  73.         $criteria = new Criteria();
  74.         $criteria->addFilter(new EqualsFilter('cart_token'$event->getPreviousToken()));
  75.         try {
  76.             $cartBarcode $this->cartBarcodeRepository->search($criteriaContext::createDefaultContext())->first();
  77.         } catch (\Exception $ex) {
  78.             $cartBarcode null;
  79.         }
  80.         if ($cartBarcode) {
  81.             $this->cartBarcodeRepository->update([
  82.                 [
  83.                     'id' => $cartBarcode->getId(),
  84.                     'cart_token' => $event->getCurrentToken(),
  85.                 ]
  86.             ], Context::createDefaultContext());
  87.         }
  88.     }
  89. }