|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace AreanetAlohaThemeExtended\Subscriber; |
| 4 | + |
| 5 | +use Shopware\Core\Content\Product\AbstractIsNewDetector; |
| 6 | +use Shopware\Core\Content\Product\AbstractProductMaxPurchaseCalculator; |
| 7 | +use Shopware\Core\Content\Product\AbstractProductVariationBuilder; |
| 8 | +use Shopware\Core\Content\Product\AbstractPropertyGroupSorter; |
| 9 | +use Shopware\Core\Content\Product\DataAbstractionLayer\CheapestPrice\CheapestPriceContainer; |
| 10 | +use Shopware\Core\Content\Product\ProductDefinition; |
| 11 | +use Shopware\Core\Content\Product\ProductEvents; |
| 12 | +use Shopware\Core\Content\Product\SalesChannel\Price\AbstractProductPriceCalculator; |
| 13 | +use Shopware\Core\Framework\DataAbstractionLayer\Entity; |
| 14 | +use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent; |
| 15 | +use Shopware\Core\Framework\Log\Package; |
| 16 | +use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent; |
| 17 | +use Shopware\Core\System\SystemConfig\SystemConfigService; |
| 18 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +#[Package('inventory')] |
| 24 | +class ProductSubscriber implements EventSubscriberInterface |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @internal |
| 28 | + */ |
| 29 | + public function __construct( |
| 30 | + private readonly AbstractProductVariationBuilder $productVariationBuilder, |
| 31 | + private readonly AbstractProductPriceCalculator $calculator, |
| 32 | + private readonly AbstractPropertyGroupSorter $propertyGroupSorter, |
| 33 | + private readonly SystemConfigService $systemConfigService |
| 34 | + ) |
| 35 | + { |
| 36 | + } |
| 37 | + |
| 38 | + public static function getSubscribedEvents(): array |
| 39 | + { |
| 40 | + return [ |
| 41 | + 'sales_channel.' . ProductEvents::PRODUCT_LOADED_EVENT => 'salesChannelLoaded', |
| 42 | + 'sales_channel.product.partial_loaded' => 'salesChannelLoaded', |
| 43 | + ]; |
| 44 | + } |
| 45 | + |
| 46 | + public function salesChannelLoaded(SalesChannelEntityLoadedEvent $event): void |
| 47 | + { |
| 48 | + |
| 49 | + $salesChannelContext = $event->getSalesChannelContext(); |
| 50 | + $isEnabled = $this->systemConfigService->get('AreanetAlohaThemeExtended.config.mergeOptionsAndProperties', $salesChannelContext->getSalesChannelId()); |
| 51 | + |
| 52 | + if (!$isEnabled) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + foreach ($event->getEntities() as $product) { |
| 57 | + |
| 58 | + $assigns = []; |
| 59 | + |
| 60 | + $properties = $product->get('properties') ? $product->get('properties') : null; |
| 61 | + $options = $product->get('properties') ? $product->get('options') : null; |
| 62 | + if ($properties) { |
| 63 | + if($options) $properties->merge($options); |
| 64 | + $assigns['sortedProperties'] = $this->propertyGroupSorter->sort($properties); |
| 65 | + }elseif ($options){ |
| 66 | + $assigns['sortedProperties'] = $this->propertyGroupSorter->sort($options); |
| 67 | + } |
| 68 | + |
| 69 | + $product->assign($assigns); |
| 70 | + |
| 71 | + $this->productVariationBuilder->build($product); |
| 72 | + } |
| 73 | + |
| 74 | + $this->calculator->calculate($event->getEntities(), $event->getSalesChannelContext()); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments