<?php declare(strict_types=1);
namespace ThemeOkeOnline\Core\Content\Category\Subscriber;
use Nette\Utils\DateTime;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\Category\CategoryEvents;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\Product\ProductEvents;
class NewCategorySubscriber implements EventSubscriberInterface
{
private $categoryRepository;
public function __construct(
EntityRepository $categoryRepository
)
{
$this->categoryRepository = $categoryRepository;
}
public static function getSubscribedEvents(): array
{
return [
CategoryEvents::CATEGORY_WRITTEN_EVENT => 'onCategoryWritten'
];
}
public function onCategoryWritten(EntityWrittenEvent $event)
{
$results = $event->getWriteResults();
/** @var EntityWriteResult $result */
foreach ($results as $result)
{
if($result->getOperation() == EntityWriteResult::OPERATION_INSERT) {
$categoryId = $result->getPrimaryKey();
/** @var CategoryEntity $currentCategory */
$currentCategory = $this->categoryRepository->search(new Criteria([$categoryId]), $event->getContext());
if (!$currentCategory->hasExtension('info')) {
$this->categoryRepository->update(
[
[
'id' => $categoryId,
'info' => ['intro' => '']
]
], $event->getContext());
}
}
}
}
}