custom/plugins/SwagB2bPlatform/components/InStock/BridgePlatform/InStockCheckoutSubscriber.php line 50

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\InStock\BridgePlatform;
  3. use Shopware\B2B\Common\IdValue;
  4. use Shopware\B2B\InStock\Framework\InStockRepository;
  5. use Shopware\B2B\InStock\Framework\InStockSearchStruct;
  6. use Shopware\B2B\StoreFrontAuthentication\Framework\AuthenticationService;
  7. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  8. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  9. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
  10. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use function array_flip;
  13. class InStockCheckoutSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var AuthenticationService
  17.      */
  18.     private $authenticationService;
  19.     /**
  20.      * @var InStockRepository
  21.      */
  22.     private $inStockRepository;
  23.     /**
  24.      * @var InStockBridgeRepository
  25.      */
  26.     private $inStockBridgeRepository;
  27.     public function __construct(
  28.         AuthenticationService $authenticationService,
  29.         InStockRepository $inStockRepository,
  30.         InStockBridgeRepository $inStockBridgeRepository
  31.     ) {
  32.         $this->authenticationService $authenticationService;
  33.         $this->inStockRepository $inStockRepository;
  34.         $this->inStockBridgeRepository $inStockBridgeRepository;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             CheckoutOrderPlacedEvent::class => 'updateInStockAfterOrderPlaced',
  40.         ];
  41.     }
  42.     public function updateInStockAfterOrderPlaced(CheckoutOrderPlacedEvent $event): void
  43.     {
  44.         if (!$this->authenticationService->isB2b()) {
  45.             return;
  46.         }
  47.         /** @var OrderLineItemCollection $lineItems */
  48.         $lineItems $event->getOrder()->getLineItems();
  49.         $productIds $lineItems->fmap(function (OrderLineItemEntity $lineItem) {
  50.             if ($lineItem->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE) {
  51.                 return null;
  52.             }
  53.             return $lineItem->getReferencedId();
  54.         });
  55.         $inStocks $this->inStockBridgeRepository->fetchInStocksByProductIds(
  56.             IdValue::fromHexToBytesList($productIds),
  57.             $this->authenticationService->getIdentity(),
  58.             new InStockSearchStruct()
  59.         );
  60.         $lineItemIds array_flip($productIds);
  61.         $matchedInStocks = [];
  62.         foreach ($inStocks as $productId => $inStock) {
  63.             $lineItem $lineItems->get($lineItemIds[$productId]);
  64.             $inStock->inStock -= $lineItem->getQuantity();
  65.             $matchedInStocks[] = $inStock;
  66.         }
  67.         if (!$matchedInStocks) {
  68.             return;
  69.         }
  70.         $this->inStockRepository->updateMultipleInStocks($matchedInStocks);
  71.     }
  72. }