vendor/crehler/mojebambino-engine/src/Subscriber/ProductPriceRangeSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. namespace Crehler\MojeBambinoEngine\Subscriber;
  3. use Crehler\MojeBambinoEngine\Service\ProductPriceRangeService;
  4. use Shopware\Core\Content\Product\ProductEvents;
  5. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class ProductPriceRangeSubscriber implements EventSubscriberInterface
  13. {
  14.     private ProductPriceRangeService $productPriceRange;
  15.     private bool $skip false;
  16.     public function __construct(ProductPriceRangeService $productPriceRange)
  17.     {
  18.         $this->productPriceRange $productPriceRange;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             ProductEvents::PRODUCT_WRITTEN_EVENT => 'onProductWritten',
  24. //            ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded'
  25.         ];
  26.     }
  27.     public function onProductLoaded(EntityLoadedEvent $event)
  28.     {
  29. //        $products = $event->getEntities();
  30. //        dd($products);
  31. //
  32. //        /** @var SalesChannelProductEntity $product */
  33. //        foreach ($products as $product) {
  34. //            if ($product instanceof SalesChannelProductEntity && !$product->hasExtension('priceRange')) {
  35. //                $product->addExtension('priceRange', $this->productStatusService->getProductPrices($product, $event->getContext()));
  36. //            }
  37. //        }
  38.     }
  39.     public function onProductWritten(EntityWrittenEvent $event)
  40.     {
  41.         try {
  42.             if ($this->skip) return;
  43.             $this->skip true;
  44.             foreach ($event->getWriteResults() as $elements) {
  45.                 $id $elements->getPrimaryKey();
  46.                 $this->productPriceRange->getProductPrices($id$event->getContext());
  47.             }
  48.         } catch (\Throwable $exception) {
  49.             dd($exception);
  50.         }
  51.     }
  52. }