custom/plugins/fourtwosixShowVariantsInTable6/src/Subscriber/ProductDetails.php line 34
<?phpdeclare(strict_types=1);namespace fourtwosix\ShowVariantsInTable6\Subscriber;use Shopware\Core\Content\Product\ProductCollection;use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;use Shopware\Core\System\SalesChannel\SalesChannelContext;use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;use Symfony\Component\EventDispatcher\EventSubscriberInterface;class ProductDetails implements EventSubscriberInterface{private $productRepository;private $systemConfigService;public function __construct($productRepository, $systemConfigService){$this->productRepository = $productRepository;$this->systemConfigService = $systemConfigService;}public static function getSubscribedEvents(): array{return ['Shopware\Storefront\Page\Product\ProductPageLoadedEvent' => 'onProductDetails'];}public function onProductDetails(ProductPageLoadedEvent $event){$product = $event->getPage()->getProduct();$productParentId = $product->getParentId();$salesChannelContext = $event->getSalesChannelContext();$salesChannelId = $salesChannelContext->getSalesChannelId();$data = $this->getPluginSettings($salesChannelId);if ($productParentId) {$products = $this->getProductsByParentId($salesChannelContext, $productParentId);$data["fourtwosixVariantsPrices"] = $this->getVariantsPrices($products);}$extensions = $product->getExtensions();$extensions['variantStruct'] = $data;$product->setExtensions($extensions);}private function getProductsByParentId(SalesChannelContext &$context, string &$parentId): ProductCollection{$criteria = new Criteria();$criteria->addFilter(new EqualsFilter("parentId", $parentId));return $this->productRepository->search($criteria, $context)->getEntities();}private function getPluginSettings(string &$salesChannelId): array{$data['fourtwosixVariantsToDisplay'] = $this->getPluginSetting($salesChannelId,'fourtwosixShowVariantsInTable6.config.variantsToDisplay');$data['fourtwosixShowTableTitles'] = $this->getPluginSetting($salesChannelId,'fourtwosixShowVariantsInTable6.config.showTableTitles');$data['fourtwosixShowListPrices'] = $this->getPluginSetting($salesChannelId,'fourtwosixShowVariantsInTable6.config.showListPrices');$data['fourtwosixShowScaleUnit'] = $this->getPluginSetting($salesChannelId,'fourtwosixShowVariantsInTable6.config.showScaleUnit');$data['fourtwosixShowDiscountPercent'] = $this->getPluginSetting($salesChannelId,'fourtwosixShowVariantsInTable6.config.showDiscountPercent');return $data;}private function getPluginSetting(string &$salesChannelId, string $key){return $this->systemConfigService->get($key, $salesChannelId);}private function getVariantsPrices(ProductCollection &$products){$variantsPrices = [];foreach ($products as $item) {if (empty($optionIds = $item->getOptionIds())) {continue;}$calculatedPrice = $item->getCalculatedPrice();foreach ($optionIds as $optionId) {$variantsPrices[$optionId]['unitPrice'] = $calculatedPrice->getUnitPrice();$variantsPrices[$optionId]['listPrice'] = $calculatedPrice->getListPrice();$variantsPrices[$optionId]['referencePrice'] = $calculatedPrice->getReferencePrice();}}return $variantsPrices;}}