custom/plugins/Neon6Configurator/src/Subscribers/OnPageLoaded.php line 59

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Neon\Configurator\Subscribers;
  3. use Neon\Configurator\Neon6Configurator;
  4. use Neon\Configurator\Services\Calculators\CalculationContext;
  5. use Neon\Configurator\Services\Calculators\DeliverytimeCalculator;
  6. use Neon\Configurator\Services\ConfigurationLoader;
  7. use Shopware\Core\Content\Cms\CmsPageEntity;
  8. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  9. use Shopware\Core\Content\Cms\SalesChannel\Struct\BuyBoxStruct;
  10. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductBoxStruct;
  11. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductSliderStruct;
  12. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  13. use Shopware\Core\System\DeliveryTime\DeliveryTimeEntity;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Shopware\Storefront\Page\PageLoadedEvent;
  16. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class OnPageLoaded implements EventSubscriberInterface
  19. {
  20.     /** @var ConfigurationLoader */
  21.     private $configurationLoader;
  22.     /** @var DeliverytimeCalculator */
  23.     private $deliverytimeCalculator;
  24.     private $fieldDataBuilder;
  25.     /**
  26.      * @inheritDoc
  27.      */
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             ProductPageLoadedEvent::class => 'onPageLoaded',
  32.             CmsPageLoadedEvent::class => 'onCmsPageLoaded'
  33.         ];
  34.     }
  35.     public function __construct(ConfigurationLoader $configurationLoader,
  36.                                 $deliverytimeCalculator,
  37.                                 $fieldDataBuilder)
  38.     {
  39.         $this->configurationLoader $configurationLoader;
  40.         $this->deliverytimeCalculator $deliverytimeCalculator;
  41.         $this->fieldDataBuilder $fieldDataBuilder;
  42.     }
  43.     public function onPageLoaded(PageLoadedEvent $event)
  44.     {
  45.         /**
  46.          * @var SalesChannelProductEntity $product
  47.          */
  48.         $product $event->getPage()->getProduct();
  49.         $this->loadConfigurationFromCustomField($product$event->getSalesChannelContext());
  50.     }
  51.     public function onCmsPageLoaded(CmsPageLoadedEvent $event)
  52.     {
  53.         /** @var CmsPageEntity $r */
  54.         foreach ($event->getResult() as $r) {
  55.             foreach ($r->getSections() as $s) {
  56.                 foreach ($s->getBlocks() as $b) {
  57.                     foreach ($b->getSlots() as $sl) {
  58.                         if ($sl->getData() instanceof ProductBoxStruct ||
  59.                             $sl->getData() instanceof BuyBoxStruct) {
  60.                             /** @var ProductBoxStruct $productStruct */
  61.                             $productStruct $sl->getData();
  62.                             $this->loadConfigurationFromCustomField($productStruct->getProduct(), $event->getSalesChannelContext());
  63.                         }
  64.                         if ($sl->getData() instanceof ProductSliderStruct) {
  65.                             /** @var ProductSliderStruct $productSliderStruct */
  66.                             $productSliderStruct $sl->getData();
  67.                             foreach ($productSliderStruct->getProducts() as $product) {
  68.                                 $this->loadConfigurationFromCustomField($product$event->getSalesChannelContext());
  69.                             }
  70.                         }
  71.                     }
  72.                 }
  73.             }
  74.         }
  75.     }
  76.     /**
  77.      * @param SalesChannelProductEntity|null $product
  78.      * @param SalesChannelContext $context
  79.      * @return void
  80.      * @throws \Twig\Error\LoaderError
  81.      * @throws \Twig\Error\SyntaxError
  82.      */
  83.     private function loadConfigurationFromCustomField($productSalesChannelContext $context): void
  84.     {
  85.         if ($product != null) {
  86.             $configuration $this->configurationLoader->loadFullConfigurationByProduct($product$context);
  87.             $customFields $product->getCustomFields();
  88.             $customFields[Neon6Configurator::CUSTOMFIELD_PRODUCTS_CONFIG] = $configuration;
  89.             $product->setCustomFields($customFields);
  90.             if ($configuration != null) {
  91.                 $calculationContext = new CalculationContext($product,
  92.                     $product->getCalculatedPrice()->getUnitPrice(),
  93.                     $product->getExtension('neon_configurator')->get('properties'),
  94.                     $context->getCurrentCustomerGroup(),
  95.                     $this->fieldDataBuilder->buildFromDefaultValues($configuration->getFields(), $product),
  96.                     $product->getMinPurchase(),
  97.                     $configuration->getCommoncalculations(),
  98.                     $context);
  99.                 $deliveryTime $this->deliverytimeCalculator->calculatetDeliverytime($configuration$calculationContext);
  100.                 if ($deliveryTime !== null) {
  101.                     $deliveryTimeEntity = new DeliveryTimeEntity();
  102.                     $deliveryTimeEntity->setMin($deliveryTime->getMin());
  103.                     $deliveryTimeEntity->setMax($deliveryTime->getMax());
  104.                     $deliveryTimeEntity->setUnit($deliveryTime->getUnit());
  105.                     $product->setDeliveryTime($deliveryTimeEntity);
  106.                 }
  107.             }
  108.         }
  109.     }
  110. }