custom/plugins/NeonPlatformUnitarticles/src/Subscribers/OnPageLoaded.php line 88

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Neon\Unitarticles\Subscribers;
  3. use Neon\Unitarticles\Events\UnitarticlesEditFormLoadedEvent;
  4. use Neon\Unitarticles\NeonPlatformUnitarticles;
  5. use Neon\Unitarticles\Services\PriceFactorService;
  6. use Shopware\Core\Checkout\Customer\Event\CustomerWishlistProductListingResultEvent;
  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\Events\ProductCrossSellingsLoadedEvent;
  13. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  14. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  15. use Shopware\Core\Content\Product\Events\ProductSuggestResultEvent;
  16. use Shopware\Core\Content\Product\ProductEntity;
  17. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  18. use Shopware\Storefront\Page\PageLoadedEvent;
  19. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  20. use Shopware\Storefront\Page\Wishlist\WishlistPageLoadedEvent;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. class OnPageLoaded implements EventSubscriberInterface
  23. {
  24.     /** @var PriceFactorService */
  25.     private $priceFactorService;
  26.     /**
  27.      * @inheritDoc
  28.      */
  29.     public static function getSubscribedEvents()
  30.     {
  31.         return [
  32.             ProductPageLoadedEvent::class => 'onPageLoaded',
  33.             'Swag\CmsExtensions\Storefront\Pagelet\Quickview\QuickviewPageletLoadedEvent' => 'onPageletLoaded',
  34.             ProductListingResultEvent::class => 'onProductListingResultCreated',
  35.             ProductSearchResultEvent::class => 'onProductSearchResult',
  36.             ProductSuggestResultEvent::class => 'onProductSuggestPageLoaded',
  37.             ProductCrossSellingsLoadedEvent::class => 'onCrossSellingLoaded',
  38.             CmsPageLoadedEvent::class => 'onCmsPageLoaded',
  39.             UnitarticlesEditFormLoadedEvent::class => 'onUnitarticlesEditFormLoaded',
  40.             CustomerWishlistProductListingResultEvent::class => 'onCustomerWishlistLoaded'
  41.         ];
  42.     }
  43.     public function __construct($priceFactorService)
  44.     {
  45.         $this->priceFactorService $priceFactorService;
  46.     }
  47.     public function onUnitarticlesEditFormLoaded(UnitarticlesEditFormLoadedEvent $event)
  48.     {
  49.         $this->adjustCustomFields($event->getProduct());
  50.     }
  51.     public function onPageLoaded(PageLoadedEvent $event)
  52.     {
  53.         $this->adjustCustomFields($event->getPage()->getProduct());
  54.     }
  55.     public function onPageletLoaded($event)
  56.     {
  57.         $this->adjustCustomFields($event->getPagelet()->getProduct());
  58.     }
  59.     public function onCustomerWishlistLoaded(CustomerWishlistProductListingResultEvent $event)
  60.     {
  61.         $this->adjustCustomFieldsForProducts($event->getResult()->getEntities());
  62.     }
  63.     public function onProductListingResultCreated(ProductListingResultEvent $event)
  64.     {
  65.         $this->adjustCustomFieldsForProducts($event->getResult()->getEntities());
  66.     }
  67.     public function onProductSearchResult(ProductSearchResultEvent $event)
  68.     {
  69.         $this->adjustCustomFieldsForProducts($event->getResult()->getEntities());
  70.     }
  71.     public function onProductSuggestPageLoaded(ProductSuggestResultEvent $event)
  72.     {
  73.         $this->adjustCustomFieldsForProducts($event->getResult()->getEntities());
  74.     }
  75.     public function onCrossSellingLoaded(ProductCrossSellingsLoadedEvent $event)
  76.     {
  77.         foreach ($event->getCrossSellings()->getElements() as $element) {
  78.             $this->adjustCustomFieldsForProducts($element->getProducts());
  79.         }
  80.     }
  81.     public function onCmsPageLoaded(CmsPageLoadedEvent $event)
  82.     {
  83.         /** @var CmsPageEntity $r */
  84.         foreach ($event->getResult() as $r) {
  85.             foreach ($r->getSections() as $s) {
  86.                 foreach ($s->getBlocks() as $b) {
  87.                     foreach ($b->getSlots() as $sl) {
  88.                         if ($sl->getData() instanceof ProductBoxStruct ||
  89.                             $sl->getData() instanceof BuyBoxStruct) {
  90.                             /** @var ProductBoxStruct $productStruct */
  91.                             $productStruct $sl->getData();
  92.                             $this->adjustCustomFields($productStruct->getProduct());
  93.                         }
  94.                         if ($sl->getData() instanceof ProductSliderStruct) {
  95.                             /** @var ProductSliderStruct $productSliderStruct */
  96.                             $productSliderStruct $sl->getData();
  97.                             /** @var ProductEntity $productStruct */
  98.                             foreach ($productSliderStruct->getProducts() as $productStruct) {
  99.                                 $this->adjustCustomFields($productStruct);
  100.                             }
  101.                         }
  102.                     }
  103.                 }
  104.             }
  105.         }
  106.     }
  107.     /**
  108.      * @param $entities
  109.      */
  110.     private function adjustCustomFieldsForProducts($entities): void
  111.     {
  112.         foreach ($entities as $product) {
  113.             $this->adjustCustomFields($product);
  114.         }
  115.     }
  116.     /**
  117.      * @param SalesChannelProductEntity|ProductEntity $product
  118.      */
  119.     private function adjustCustomFields($product): void
  120.     {
  121.         if ($product !== null && array_key_exists(NeonPlatformUnitarticles::CUSTOMFIELD_ISUNITARTICLE$product->getCustomFields())) {
  122.             $this->addFactorToCustomFields($product);
  123.             $this->limitMaxAmountToAvailableStock($product);
  124.         }
  125.     }
  126.     /**
  127.      * @param SalesChannelProductEntity|ProductEntity $product
  128.      */
  129.     private function addFactorToCustomFields($product)
  130.     {
  131.         $customFields $product->getCustomFields();
  132.         $customFields[NeonPlatformUnitarticles::CUSTOMFIELD_CALCULATEDPRICEFACTOR] = $this->priceFactorService->calculateWithCustomFields($customFields);
  133.         $product->setCustomFields($customFields);
  134.     }
  135.     /**
  136.      * @param SalesChannelProductEntity $product
  137.      */
  138.     private function limitMaxAmountToAvailableStock($product)
  139.     {
  140.         $customFields $product->getCustomFields();
  141.         $maxPurchase $product->getCalculatedMaxPurchase() / $customFields[NeonPlatformUnitarticles::CUSTOMFIELD_CALCULATEDPRICEFACTOR];
  142.         if (array_key_exists(NeonPlatformUnitarticles::CUSTOMFIELD_ISUNITARTICLE$customFields)
  143.             && (array_key_exists(NeonPlatformUnitarticles::CUSTOMFIELD_HIDEQUANTITYSELECTION$customFields) && $customFields[NeonPlatformUnitarticles::CUSTOMFIELD_HIDEQUANTITYSELECTION])
  144.             && (array_key_exists(NeonPlatformUnitarticles::CUSTOMFIELD_MAXVALUE$customFields) && $customFields[NeonPlatformUnitarticles::CUSTOMFIELD_MAXVALUE] > $maxPurchase)) {
  145.             $customFields[NeonPlatformUnitarticles::CUSTOMFIELD_MAXVALUE] = $maxPurchase;
  146.             $product->setCustomFields($customFields);
  147.         }
  148.         if (array_key_exists(NeonPlatformUnitarticles::CUSTOMFIELD_MINVALUE$customFields) &&
  149.             array_key_exists(NeonPlatformUnitarticles::CUSTOMFIELD_MAXVALUE$customFields) &&
  150.             $customFields[NeonPlatformUnitarticles::CUSTOMFIELD_MINVALUE] > $customFields[NeonPlatformUnitarticles::CUSTOMFIELD_MAXVALUE]) {
  151.             $product->setAvailableStock(0);
  152.         }
  153.     }
  154. }