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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\Subscriber;
  3. use Shopware\Core\Content\Product\ProductEntity;
  4. use Shopware\Core\Content\Product\ProductEvents;
  5. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  6. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionCollection;
  7. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionEntity;
  8. use Shopware\Core\Content\Property\PropertyGroupCollection;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ProductSubscriber implements EventSubscriberInterface
  12. {
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  16.         return [
  17.             ProductEvents::PRODUCT_LOADED_EVENT => [
  18.                 ['loaded'],
  19.             ],
  20.         ];
  21.     }
  22.     public function loaded(EntityLoadedEvent $event): void
  23.     {
  24.         /** @var ProductEntity $product */
  25.         foreach ($event->getEntities() as $product) {
  26.             $product->setVariation(
  27.                 $this->buildVariation($product)
  28.             );
  29.             if ($product instanceof SalesChannelProductEntity) {
  30.                 $product->setSortedProperties(
  31.                     $this->sortProperties($product)
  32.                 );
  33.             }
  34.         }
  35.     }
  36.     private function sortProperties(SalesChannelProductEntity $product): PropertyGroupCollection
  37.     {
  38.         $properties $product->getProperties();
  39.         if ($properties === null) {
  40.             return new PropertyGroupCollection();
  41.         }
  42.         $sorted = [];
  43.         foreach ($properties as $option) {
  44.             $origin $option->getGroup();
  45.             if (!$origin) {
  46.                 continue;
  47.             }
  48.             $group = clone $origin;
  49.             $groupId $group->getId();
  50.             if (\array_key_exists($groupId$sorted)) {
  51.                 $sorted[$groupId]->getOptions()->add($option);
  52.                 continue;
  53.             }
  54.             if (!$group->getOptions()) {
  55.                 $group->setOptions(new PropertyGroupOptionCollection());
  56.             }
  57.             $group->getOptions()->add($option);
  58.             $sorted[$groupId] = $group;
  59.         }
  60.         $collection = new PropertyGroupCollection($sorted);
  61.         $collection->sortByPositions();
  62.         $collection->sortByConfig();
  63.         return $collection;
  64.     }
  65.     private function buildVariation(ProductEntity $product): array
  66.     {
  67.         if (!$product->getOptions()) {
  68.             return [];
  69.         }
  70.         $parts = [];
  71.         if (!$product->getConfiguratorGroupConfig()) {
  72.             // fallback - simply take all option names unordered
  73.             $names $product->getOptions()->map(function (PropertyGroupOptionEntity $option) {
  74.                 if (!$option->getGroup()) {
  75.                     return [];
  76.                 }
  77.                 return [
  78.                     'group' => $option->getGroup()->getTranslation('name'),
  79.                     'option' => $option->getTranslation('name'),
  80.                 ];
  81.             });
  82.             return array_values($names);
  83.         }
  84.         // collect option names in order of the configuration
  85.         foreach ($product->getConfiguratorGroupConfig() as $groupConfig) {
  86.             $option $product->getOptions()
  87.                 ->filterByGroupId($groupConfig['id'])
  88.                 ->first();
  89.             if (!$option) {
  90.                 continue;
  91.             }
  92.             if ($option->getGroup()) {
  93.                 $parts[] = [
  94.                     'group' => $option->getGroup()->getTranslation('name'),
  95.                     'option' => $option->getTranslation('name'),
  96.                 ];
  97.             }
  98.         }
  99.         return $parts;
  100.     }
  101. }