custom/static-plugins/EfbStorefront/Subscriber/ConfirmPageSubscriber.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Uandi\EFB\Storefront\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Cart;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  5. use Shopware\Core\Content\Product\ProductEntity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\Struct\ArrayStruct;
  9. use Shopware\Core\Framework\Struct\Struct;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  12. use Shopware\Storefront\Page\PageLoadedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Uandi\EFB\Storefront\Trait\DecodeStockInformation;
  15. class ConfirmPageSubscriber implements EventSubscriberInterface
  16. {
  17.     use DecodeStockInformation;
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             CheckoutConfirmPageLoadedEvent::class => 'onConfirmPageLoadedEvent'
  22.         ];
  23.     }
  24.     private EntityRepository $productRepository;
  25.     public function __construct(EntityRepository $productRepository)
  26.     {
  27.         $this->productRepository $productRepository;
  28.     }
  29.     public function onConfirmPageLoadedEvent(PageLoadedEvent $event)
  30.     {
  31.         $cart $event->getPage()->getCart();
  32.         $context $event->getSalesChannelContext();
  33.         $productIds $this->getProductIds($cart);
  34.         if (empty($productIds)) {
  35.             return;
  36.         }
  37.         $cart->addExtensions([
  38.             'stockInfo' => $this->fetchStockInformation($productIds$context),
  39.         ]);
  40.     }
  41.     private function getProductIds(Cart $cart): array
  42.     {
  43.         $products $cart->getLineItems()->filterFlatByType(LineItem::PRODUCT_LINE_ITEM_TYPE);
  44.         return array_map(function (LineItem $lineItem) {
  45.             return $lineItem->getReferencedId();
  46.         }, $products);
  47.     }
  48.     private function fetchStockInformation(array $productIdsSalesChannelContext $context): ?Struct
  49.     {
  50.         $criteria = new Criteria($productIds);
  51.         $products $this->productRepository->search($criteria$context->getContext());
  52.         return new ArrayStruct($products->map(function (ProductEntity $productEntity) use ($context) {
  53.             return $this->decodeStockInfo($productEntity$context);
  54.         }));
  55.     }
  56. }