vendor/store.shopware.com/swagb2bplatform/components/InStock/BridgePlatform/InStockCheckoutSubscriber.php line 41

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.     private AuthenticationService $authenticationService;
  16.     private InStockRepository $inStockRepository;
  17.     private InStockBridgeRepository $inStockBridgeRepository;
  18.     public function __construct(
  19.         AuthenticationService $authenticationService,
  20.         InStockRepository $inStockRepository,
  21.         InStockBridgeRepository $inStockBridgeRepository
  22.     ) {
  23.         $this->authenticationService $authenticationService;
  24.         $this->inStockRepository $inStockRepository;
  25.         $this->inStockBridgeRepository $inStockBridgeRepository;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             CheckoutOrderPlacedEvent::class => 'updateInStockAfterOrderPlaced',
  31.         ];
  32.     }
  33.     public function updateInStockAfterOrderPlaced(CheckoutOrderPlacedEvent $event): void
  34.     {
  35.         if (!$this->authenticationService->isB2b()) {
  36.             return;
  37.         }
  38.         /** @var OrderLineItemCollection $lineItems */
  39.         $lineItems $event->getOrder()->getLineItems();
  40.         $productIds $lineItems->fmap(function (OrderLineItemEntity $lineItem) {
  41.             if ($lineItem->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE) {
  42.                 return null;
  43.             }
  44.             return $lineItem->getReferencedId();
  45.         });
  46.         $inStocks $this->inStockBridgeRepository->fetchInStocksByProductIds(
  47.             IdValue::fromHexToBytesList($productIds),
  48.             $this->authenticationService->getIdentity(),
  49.             new InStockSearchStruct()
  50.         );
  51.         $lineItemIds array_flip($productIds);
  52.         $matchedInStocks = [];
  53.         foreach ($inStocks as $productId => $inStock) {
  54.             $lineItem $lineItems->get($lineItemIds[$productId]);
  55.             $inStock->inStock -= $lineItem->getQuantity();
  56.             $matchedInStocks[] = $inStock;
  57.         }
  58.         if (!$matchedInStocks) {
  59.             return;
  60.         }
  61.         $this->inStockRepository->updateMultipleInStocks($matchedInStocks);
  62.     }
  63. }