platform/src/Core/Content/Product/Subscriber/ProductSubscriber.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\Subscriber;
  3. use Shopware\Core\Content\Product\AbstractProductVariationBuilder;
  4. use Shopware\Core\Content\Product\AbstractSalesChannelProductBuilder;
  5. use Shopware\Core\Content\Product\DataAbstractionLayer\CheapestPrice\CheapestPriceContainer;
  6. use Shopware\Core\Content\Product\ProductEntity;
  7. use Shopware\Core\Content\Product\ProductEvents;
  8. use Shopware\Core\Content\Product\SalesChannel\Price\AbstractProductPriceCalculator;
  9. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  11. use Shopware\Core\Framework\Feature;
  12. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class ProductSubscriber implements EventSubscriberInterface
  15. {
  16.     private AbstractSalesChannelProductBuilder $salesChannelProductBuilder;
  17.     private AbstractProductVariationBuilder $productVariationBuilder;
  18.     private AbstractProductPriceCalculator $calculator;
  19.     public function __construct(
  20.         AbstractSalesChannelProductBuilder $salesChannelProductBuilder,
  21.         AbstractProductVariationBuilder $productVariationBuilder,
  22.         AbstractProductPriceCalculator $calculator
  23.     ) {
  24.         $this->salesChannelProductBuilder $salesChannelProductBuilder;
  25.         $this->productVariationBuilder $productVariationBuilder;
  26.         $this->calculator $calculator;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             ProductEvents::PRODUCT_LOADED_EVENT => 'loaded',
  32.             'sales_channel.' ProductEvents::PRODUCT_LOADED_EVENT => 'salesChannelLoaded',
  33.         ];
  34.     }
  35.     public function loaded(EntityLoadedEvent $event): void
  36.     {
  37.         /** @var ProductEntity $product */
  38.         foreach ($event->getEntities() as $product) {
  39.             // CheapestPrice will only be added to SalesChannelProductEntities in the Future
  40.             if (!Feature::isActive('FEATURE_NEXT_16151')) {
  41.                 $price $product->getCheapestPrice();
  42.                 if ($price instanceof CheapestPriceContainer) {
  43.                     $resolved $price->resolve($event->getContext());
  44.                     $product->setCheapestPriceContainer($price);
  45.                     $product->setCheapestPrice($resolved);
  46.                 }
  47.             }
  48.             $this->productVariationBuilder->build($product);
  49.         }
  50.     }
  51.     public function salesChannelLoaded(SalesChannelEntityLoadedEvent $event): void
  52.     {
  53.         $context $event->getSalesChannelContext();
  54.         /** @var SalesChannelProductEntity $product */
  55.         foreach ($event->getEntities() as $product) {
  56.             if (Feature::isActive('FEATURE_NEXT_16151')) {
  57.                 $price $product->getCheapestPrice();
  58.                 if ($price instanceof CheapestPriceContainer) {
  59.                     $resolved $price->resolve($event->getContext());
  60.                     $product->setCheapestPriceContainer($price);
  61.                     $product->setCheapestPrice($resolved);
  62.                 }
  63.             }
  64.             $this->salesChannelProductBuilder->build($product$context);
  65.         }
  66.         $this->calculator->calculate(
  67.             $event->getEntities(),
  68.             $event->getSalesChannelContext()
  69.         );
  70.     }
  71. }