custom/plugins/fourtwosixShowVariantsInTable6/src/Subscriber/ProductDetails.php line 34

  1. <?php
  2. declare(strict_types=1);
  3. namespace fourtwosix\ShowVariantsInTable6\Subscriber;
  4. use Shopware\Core\Content\Product\ProductCollection;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class ProductDetails implements EventSubscriberInterface
  11. {
  12.     
  13.     private $productRepository;
  14.     private $systemConfigService;
  15.     
  16.     public function __construct($productRepository$systemConfigService)
  17.     {
  18.         $this->productRepository $productRepository;
  19.         $this->systemConfigService $systemConfigService;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             'Shopware\Storefront\Page\Product\ProductPageLoadedEvent' => 'onProductDetails'
  25.         ];
  26.     }
  27.     public function onProductDetails(ProductPageLoadedEvent $event)
  28.     {
  29.         $product $event->getPage()->getProduct();
  30.         $productParentId $product->getParentId();
  31.         $salesChannelContext $event->getSalesChannelContext();
  32.         $salesChannelId $salesChannelContext->getSalesChannelId();
  33.         $data $this->getPluginSettings($salesChannelId);
  34.         
  35.         if ($productParentId) {
  36.             $products $this->getProductsByParentId($salesChannelContext$productParentId);
  37.             $data["fourtwosixVariantsPrices"] = $this->getVariantsPrices($products);
  38.         }
  39.        
  40.         $extensions $product->getExtensions();
  41.         $extensions['variantStruct'] = $data;
  42.         $product->setExtensions($extensions);
  43.     }
  44.     private function getProductsByParentId(SalesChannelContext &$contextstring &$parentId): ProductCollection
  45.     {
  46.         $criteria = new Criteria();
  47.         $criteria->addFilter(new EqualsFilter("parentId"$parentId));
  48.         return $this->productRepository->search($criteria$context)->getEntities();
  49.     }
  50.     private function getPluginSettings(string &$salesChannelId): array
  51.     {
  52.         $data['fourtwosixVariantsToDisplay'] = $this->getPluginSetting(
  53.             $salesChannelId,
  54.             'fourtwosixShowVariantsInTable6.config.variantsToDisplay'
  55.         );
  56.         $data['fourtwosixShowTableTitles'] = $this->getPluginSetting(
  57.             $salesChannelId,
  58.             'fourtwosixShowVariantsInTable6.config.showTableTitles'
  59.         );
  60.         $data['fourtwosixShowListPrices'] = $this->getPluginSetting(
  61.             $salesChannelId,
  62.             'fourtwosixShowVariantsInTable6.config.showListPrices'
  63.         );
  64.         $data['fourtwosixShowScaleUnit'] = $this->getPluginSetting(
  65.             $salesChannelId,
  66.             'fourtwosixShowVariantsInTable6.config.showScaleUnit'
  67.         );
  68.         $data['fourtwosixShowDiscountPercent'] = $this->getPluginSetting(
  69.             $salesChannelId,
  70.             'fourtwosixShowVariantsInTable6.config.showDiscountPercent'
  71.         );
  72.         return $data;
  73.     }
  74.     private function getPluginSetting(string &$salesChannelIdstring $key)
  75.     {
  76.         return  $this->systemConfigService->get($key$salesChannelId);
  77.     }
  78.     private function getVariantsPrices(ProductCollection &$products)
  79.     {
  80.         $variantsPrices = [];
  81.         foreach ($products as $item) {
  82.             if (empty($optionIds $item->getOptionIds())) {
  83.                 continue;
  84.             }
  85.             $calculatedPrice $item->getCalculatedPrice();
  86.             foreach ($optionIds as $optionId) {
  87.                 $variantsPrices[$optionId]['unitPrice'] = $calculatedPrice->getUnitPrice();
  88.                 $variantsPrices[$optionId]['listPrice'] = $calculatedPrice->getListPrice();
  89.                 $variantsPrices[$optionId]['referencePrice'] = $calculatedPrice->getReferencePrice();
  90.             }
  91.         }
  92.         return $variantsPrices;
  93.     }
  94. }