custom/plugins/PickwareErpStarter/src/Stock/ContainerOverride/StockUpdaterOverride.php line 71

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Pickware GmbH. All rights reserved.
  4.  * This file is part of software that is released under a proprietary license.
  5.  * You must not copy, modify, distribute, make publicly available, or execute
  6.  * its contents or parts thereof without express permission by the copyright
  7.  * holder, unless otherwise permitted by law.
  8.  */
  9. declare(strict_types=1);
  10. namespace Pickware\PickwareErpStarter\Stock\ContainerOverride;
  11. use Doctrine\DBAL\Connection;
  12. use Pickware\PickwareErpStarter\Stock\ProductSalesUpdater;
  13. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  14. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  15. use Shopware\Core\Checkout\Order\OrderDefinition;
  16. use Shopware\Core\Content\Product\DataAbstractionLayer\StockUpdater;
  17. use Shopware\Core\Defaults;
  18. use Shopware\Core\Framework\Context;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  21. use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
  22. /**
  23.  * Override for \Shopware\Core\Content\Product\DataAbstractionLayer\StockUpdater to disable all
  24.  * of its functionality except the updating of product.sales. Stock will be updated by our indexers.
  25.  *
  26.  * The product.sales update is kept in this subscriber. See this issue: https://github.com/pickware/shopware-plugins/issues/2852
  27.  * The update itself is done asynchronously and periodically in a scheduled task. See this isse: https://github.com/pickware/shopware-plugins/issues/3408
  28.  */
  29. class StockUpdaterOverride extends StockUpdater
  30. {
  31.     private ?Connection $connection;
  32.     private ?ProductSalesUpdater $productSalesUpdater;
  33.     /**
  34.      * @internal
  35.      * @deprecated next major version: Both arguments will be non-optional
  36.      */
  37.     public function __construct(
  38.         ?Connection $connection null,
  39.         $productSalesUpdater null
  40.     ) {
  41.         $this->connection $connection;
  42.         if ($productSalesUpdater instanceof ProductSalesUpdater) {
  43.             // Backwards compatibility: the second parameter of this subscriber (override) changed to ProductSalesUpdater
  44.             $this->productSalesUpdater $productSalesUpdater;
  45.         } else {
  46.             $this->productSalesUpdater null;
  47.         }
  48.     }
  49.     public static function getSubscribedEvents(): array
  50.     {
  51.         return [
  52.             StateMachineTransitionEvent::class => 'stateChanged',
  53.         ];
  54.     }
  55.     public function triggerChangeSet(PreWriteValidationEvent $event): void
  56.     {
  57.     }
  58.     public function lineItemWritten(EntityWrittenEvent $event): void
  59.     {
  60.     }
  61.     public function stateChanged(StateMachineTransitionEvent $event): void
  62.     {
  63.         if (!$this->connection || !$this->productSalesUpdater) {
  64.             return;
  65.         }
  66.         if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  67.             return;
  68.         }
  69.         if ($event->getEntityName() !== OrderDefinition::ENTITY_NAME) {
  70.             return;
  71.         }
  72.         $productIds $this->connection->fetchFirstColumn(
  73.             'SELECT HEX(`product_id`) FROM `order_line_item`
  74.             WHERE
  75.                 `order_line_item`.`type` = :lineItemType
  76.                 AND `order_line_item`.`version_id` = :liveVersionId
  77.                 AND `order_line_item`.`order_id` = :orderId
  78.                 AND `order_line_item`.`product_id` IS NOT NULL;',
  79.             [
  80.                 'orderId' => hex2bin($event->getEntityId()),
  81.                 'liveVersionId' => hex2bin(Defaults::LIVE_VERSION),
  82.                 'lineItemType' => LineItem::PRODUCT_LINE_ITEM_TYPE,
  83.             ],
  84.         );
  85.         $this->productSalesUpdater->addProductsToUpdateQueue($productIds);
  86.     }
  87.     public function update(array $idsContext $context): void
  88.     {
  89.     }
  90.     public function orderPlaced(CheckoutOrderPlacedEvent $event): void
  91.     {
  92.     }
  93. }