vendor/shopware/core/Content/Product/Subscriber/ProductSubscriber.php line 87

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\Subscriber;
  3. use Shopware\Core\Content\Product\AbstractIsNewDetector;
  4. use Shopware\Core\Content\Product\AbstractProductMaxPurchaseCalculator;
  5. use Shopware\Core\Content\Product\AbstractProductVariationBuilder;
  6. use Shopware\Core\Content\Product\AbstractPropertyGroupSorter;
  7. use Shopware\Core\Content\Product\AbstractSalesChannelProductBuilder;
  8. use Shopware\Core\Content\Product\DataAbstractionLayer\CheapestPrice\CheapestPriceContainer;
  9. use Shopware\Core\Content\Product\ProductEntity;
  10. use Shopware\Core\Content\Product\ProductEvents;
  11. use Shopware\Core\Content\Product\SalesChannel\Price\AbstractProductPriceCalculator;
  12. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  13. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionCollection;
  14. use Shopware\Core\Framework\Context;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Event\PartialEntityLoadedEvent;
  18. use Shopware\Core\Framework\Feature;
  19. use Shopware\Core\System\SalesChannel\Entity\PartialSalesChannelEntityLoadedEvent;
  20. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  21. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. class ProductSubscriber implements EventSubscriberInterface
  24. {
  25.     private AbstractSalesChannelProductBuilder $salesChannelProductBuilder;
  26.     private AbstractProductVariationBuilder $productVariationBuilder;
  27.     private AbstractProductPriceCalculator $calculator;
  28.     private AbstractPropertyGroupSorter $propertyGroupSorter;
  29.     private AbstractProductMaxPurchaseCalculator $maxPurchaseCalculator;
  30.     private AbstractIsNewDetector $isNewDetector;
  31.     public function __construct(
  32.         AbstractSalesChannelProductBuilder $salesChannelProductBuilder,
  33.         AbstractProductVariationBuilder $productVariationBuilder,
  34.         AbstractProductPriceCalculator $calculator,
  35.         AbstractPropertyGroupSorter $propertyGroupSorter,
  36.         AbstractProductMaxPurchaseCalculator $maxPurchaseCalculator,
  37.         AbstractIsNewDetector $isNewDetector
  38.     ) {
  39.         $this->salesChannelProductBuilder $salesChannelProductBuilder;
  40.         $this->productVariationBuilder $productVariationBuilder;
  41.         $this->calculator $calculator;
  42.         $this->propertyGroupSorter $propertyGroupSorter;
  43.         $this->maxPurchaseCalculator $maxPurchaseCalculator;
  44.         $this->isNewDetector $isNewDetector;
  45.     }
  46.     public static function getSubscribedEvents(): array
  47.     {
  48.         return [
  49.             ProductEvents::PRODUCT_LOADED_EVENT => 'loaded',
  50.             'product.partial_loaded' => 'partialEntityLoaded',
  51.             'sales_channel.' ProductEvents::PRODUCT_LOADED_EVENT => 'salesChannelLoaded',
  52.             'sales_channel.product.partial_loaded' => 'partialSalesChannelLoaded',
  53.         ];
  54.     }
  55.     public function loaded(EntityLoadedEvent $event): void
  56.     {
  57.         $this->entityLoaded($event->getEntities(), $event->getContext());
  58.     }
  59.     /**
  60.      * @internal
  61.      */
  62.     public function partialEntityLoaded(PartialEntityLoadedEvent $event): void
  63.     {
  64.         $this->entityLoaded($event->getEntities(), $event->getContext());
  65.     }
  66.     public function salesChannelLoaded(SalesChannelEntityLoadedEvent $event): void
  67.     {
  68.         $this->productSalesChannelLoaded($event->getEntities(), $event->getSalesChannelContext());
  69.     }
  70.     /**
  71.      * @internal
  72.      */
  73.     public function partialSalesChannelLoaded(PartialSalesChannelEntityLoadedEvent $event): void
  74.     {
  75.         $this->productSalesChannelLoaded($event->getEntities(), $event->getSalesChannelContext());
  76.     }
  77.     /**
  78.      * @param Entity[] $collection
  79.      */
  80.     private function entityLoaded(array $collectionContext $context): void
  81.     {
  82.         /** @var ProductEntity $product */
  83.         foreach ($collection as $product) {
  84.             // CheapestPrice will only be added to SalesChannelProductEntities in the Future
  85.             if (!Feature::isActive('FEATURE_NEXT_16151')) {
  86.                 $price $product->get('cheapestPrice');
  87.                 if ($price instanceof CheapestPriceContainer) {
  88.                     $resolved $price->resolve($context);
  89.                     $product->assign([
  90.                         'cheapestPrice' => $resolved,
  91.                         'cheapestPriceContainer' => $price,
  92.                     ]);
  93.                 }
  94.             }
  95.             $this->productVariationBuilder->build($product);
  96.         }
  97.     }
  98.     /**
  99.      * @param Entity[] $elements
  100.      */
  101.     private function productSalesChannelLoaded(array $elementsSalesChannelContext $context): void
  102.     {
  103.         /** @var SalesChannelProductEntity $product */
  104.         foreach ($elements as $product) {
  105.             if (Feature::isActive('FEATURE_NEXT_16151')) {
  106.                 $price $product->get('cheapestPrice');
  107.                 if ($price instanceof CheapestPriceContainer) {
  108.                     $resolved $price->resolve($context->getContext());
  109.                     $product->assign([
  110.                         'cheapestPrice' => $resolved,
  111.                         'cheapestPriceContainer' => $price,
  112.                     ]);
  113.                 }
  114.             }
  115.             if (Feature::isActive('v6_5_0_0')) {
  116.                 $assigns = [];
  117.                 if (($properties $product->get('properties')) !== null && $properties instanceof PropertyGroupOptionCollection) {
  118.                     $assigns['sortedProperties'] = $this->propertyGroupSorter->sort($properties);
  119.                 }
  120.                 $assigns['calculatedMaxPurchase'] = $this->maxPurchaseCalculator->calculate($product$context);
  121.                 $assigns['isNew'] = $this->isNewDetector->isNew($product$context);
  122.                 $product->assign($assigns);
  123.             } else {
  124.                 $this->salesChannelProductBuilder->build($product$context);
  125.             }
  126.         }
  127.         $this->calculator->calculate($elements$context);
  128.     }
  129. }