custom/plugins/IcebergStorefrontChildTheme/src/Subscriber/CartSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace IcebergStorefrontChildTheme\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  4. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. /**
  9.  * In store front we need to know base price for each product in cart.
  10.  * This subscriber adds it.
  11.  */
  12. class CartSubscriber implements EventSubscriberInterface
  13. {
  14.     private SalesChannelRepository $productRepository;
  15.     public function __construct(SalesChannelRepository $productRepository)
  16.     {
  17.         $this->productRepository $productRepository;
  18.     }
  19.     public static function getSubscribedEvents()
  20.     {
  21.         return [
  22.             BeforeLineItemAddedEvent::class => 'beforeLineItemAdded'
  23.         ];
  24.     }
  25.     public function beforeLineItemAdded(BeforeLineItemAddedEvent $beforeLineItemAddedEvent)
  26.     {
  27.         $context $beforeLineItemAddedEvent->getSalesChannelContext();
  28.         $lineItem $beforeLineItemAddedEvent->getLineItem();
  29.         if ($lineItem->getType() !== "product")
  30.             return;
  31.         /** @var SalesChannelProductEntity $product */
  32.         $product $this->productRepository->search(new Criteria([$lineItem->getReferencedId()]), $context)->first();
  33.         if ($product === null)
  34.             return;
  35.         $prices $product->getPrices();
  36.         $prices->sortByQuantity();
  37.         $lineItem->setPayloadValue('basePrice'$prices->first()->getPrice()->first()->getGross());
  38.     }
  39. }