custom/plugins/ProcAvailableStock/src/Subscriber/AvailableStockSubscriber.php line 88

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Proc\ProcAvailableStock\Subscriber;
  3. use Proc\ProcAvailableStock\Helper\ErrorHandler;
  4. use Shopware\Core\Content\Product\ProductEntity;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Storefront\Page\PageLoadedEvent;
  11. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  12. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  13. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  14. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  15. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. /**
  18.  * Class AvailableStockSubscriber
  19.  * @package Proc\ProcAvailableStock\Subscriber
  20.  */
  21. class AvailableStockSubscriber implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var PageLoadedEvent
  25.      */
  26.     private PageLoadedEvent $event;
  27.     /**
  28.      * @var EntityRepositoryInterface
  29.      */
  30.     private EntityRepositoryInterface $productRepository;
  31.     /**
  32.      * @inheritDoc
  33.      */
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37. //            OffcanvasCartPageLoadedEvent::class => ['onCartLoaded', 1],
  38.             CheckoutCartPageLoadedEvent::class => ['onCartLoaded'1],
  39.             CheckoutConfirmPageLoadedEvent::class => ['onCartLoaded'1],
  40.             CheckoutFinishPageLoadedEvent::class => ['onFinishLoaded'1]
  41.         ];
  42.     }
  43.     /**
  44.      * @param SystemConfigService $config
  45.      * @param EntityRepositoryInterface $repositoryInterface
  46.      */
  47.     function __construct(SystemConfigService $configEntityRepositoryInterface $repositoryInterface)
  48.     {
  49.         $this->productRepository $repositoryInterface;
  50.     }
  51.     /**
  52.      * @param PageLoadedEvent $event
  53.      */
  54.     public function onCartLoaded(PageLoadedEvent $event)
  55.     {
  56.         $this->event $event;
  57.         if (
  58. //            !($this->event instanceof OffcanvasCartPageLoadedEvent) &&
  59.             !($this->event instanceof CheckoutCartPageLoadedEvent) &&
  60.             !($this->event instanceof CheckoutConfirmPageLoadedEvent)
  61.         ) return;
  62.         if (!$this->checkLogin()) return;
  63.         $lineItems $this->event->getPage()->getCart()->getLineItems()->filterType(LineItem::PRODUCT_LINE_ITEM_TYPE);
  64.         if (count($lineItems) < 1) return;
  65.         foreach ($lineItems as $lineItem)
  66.         {
  67.             $product $this->getProduct($lineItem->getReferencedId());
  68.             $lineItem->setExtensions(['availability' => $product->getStock() - $lineItem->getQuantity()]);
  69.         }
  70.     }
  71.     /**
  72.      * @param PageLoadedEvent $event
  73.      */
  74.     public function onFinishLoaded(PageLoadedEvent $event)
  75.     {
  76.         $this->event $event;
  77.         if (!($this->event instanceof CheckoutFinishPageLoadedEvent)) return;
  78.         if (!$this->checkLogin()) return;
  79.         $lineItems $this->event->getPage()->getOrder()->getLineItems()->filterByType(LineItem::PRODUCT_LINE_ITEM_TYPE);
  80.         if (count($lineItems) < 1) return;
  81.         foreach ($lineItems as $lineItem)
  82.         {
  83.             $product $this->getProduct($lineItem->getReferencedId());
  84.             $lineItem->setExtensions(['availability' => $product->getStock() - $lineItem->getQuantity()]);
  85.         }
  86.     }
  87.     /**
  88.      * @param string $productID
  89.      * @return ProductEntity
  90.      */
  91.     public function getProduct(string $productID) : ProductEntity
  92.     {
  93.         $criteria = new Criteria([$productID]);
  94.         /** @var EntityCollection $products */
  95.         $products $this->productRepository->search($criteriaContext::createDefaultContext());
  96.         $productList $products->getElements();
  97.         /** @var ProductEntity $product */
  98.         $product $productList[$productID];
  99.         return $product;
  100.     }
  101.     /**
  102.      * @return bool
  103.      */
  104.     private function checkLogin() : bool
  105.     {
  106.         if ($this->event->getSalesChannelContext()->getCustomer() === null)
  107.         {
  108.             $this->error = (new ErrorHandler)->error(ErrorHandler::NO_CUSTOMER_ERROR);
  109.             return false;
  110.         }
  111.         return true;
  112.     }
  113. }