<?php
namespace Crehler\MojeBambinoEngine\Subscriber;
use Crehler\MojeBambinoEngine\Service\ProductPriceRangeService;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductPriceRangeSubscriber implements EventSubscriberInterface
{
private ProductPriceRangeService $productPriceRange;
private bool $skip = false;
public function __construct(ProductPriceRangeService $productPriceRange)
{
$this->productPriceRange = $productPriceRange;
}
public static function getSubscribedEvents(): array
{
return [
ProductEvents::PRODUCT_WRITTEN_EVENT => 'onProductWritten',
// ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded'
];
}
public function onProductLoaded(EntityLoadedEvent $event)
{
// $products = $event->getEntities();
// dd($products);
//
// /** @var SalesChannelProductEntity $product */
// foreach ($products as $product) {
// if ($product instanceof SalesChannelProductEntity && !$product->hasExtension('priceRange')) {
// $product->addExtension('priceRange', $this->productStatusService->getProductPrices($product, $event->getContext()));
// }
// }
}
public function onProductWritten(EntityWrittenEvent $event)
{
try {
if ($this->skip) return;
$this->skip = true;
foreach ($event->getWriteResults() as $elements) {
$id = $elements->getPrimaryKey();
$this->productPriceRange->getProductPrices($id, $event->getContext());
}
} catch (\Throwable $exception) {
dd($exception);
}
}
}