<?php
declare (strict_types=1);
namespace Crehler\MojeBambinoEngine\Subscriber;
use Crehler\MojeBambinoEngine\Service\CrehlerContextService;
use Shopware\Core\Content\Category\Event\NavigationLoadedEvent;
use Shopware\Core\Content\Product\Aggregate\ProductConfiguratorSetting\ProductConfiguratorSettingEntity;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionEntity;
use Shopware\Core\Framework\Api\Sync\SyncBehavior;
use Shopware\Core\Framework\Api\Sync\SyncOperation;
use Shopware\Core\Framework\Api\Sync\SyncServiceInterface;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
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\Framework\Uuid\Uuid;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ImportProductAddSubscriber implements EventSubscriberInterface
{
private EntityRepository $productRepository;
private EntityRepository $configuratorSettingsRepository;
private CrehlerContextService $crehlerContext;
public function __construct(EntityRepository $productRepository, EntityRepository $configuratorRepository, CrehlerContextService $crehlerContext)
{
$this->productRepository = $productRepository;
$this->configuratorSettingsRepository = $configuratorRepository;
$this->crehlerContext = $crehlerContext;
}
public static function getSubscribedEvents(): array
{
return [
ProductEvents::PRODUCT_WRITTEN_EVENT => 'onProductWritten'
];
}
public function onProductWritten(EntityWrittenEvent $event)
{
try {
if ($this->crehlerContext->get(CrehlerContextService::CREHLER_PRODUCT_IMPORT_COMMAND, false)) return;
foreach ($event->getWriteResults() as $result) {
if (empty($result->getPayload()) || !isset($result->getPayload()['id'])) {
continue;
}
$criteria = new Criteria([$result->getPayload()['id']]);
$criteria->addAssociation('options');
/** @var ProductEntity $productEntity */
$productEntity = $this->productRepository->search($criteria, Context::createDefaultContext())->first();
if (!$productEntity || !$productEntity->getParentId() || !isset($result->getPayload()['parentId'])) {
continue;
}
if (!$productEntity->getOptionIds() || empty($productEntity->getOptionIds())) {
if (isset($result->getPayload()['options']) && count($result->getPayload()['options']) > 0) {
continue;
}
$this->addOptionsIds($productEntity);
}
}
} catch (\Throwable $exception) {
dd($exception);
}
}
private function addOptionsIds(ProductEntity $productEntity): void
{
$optionsIds = [];
foreach ($productEntity->getOptions() as $option) {
$optionsIds[] = ['id' => $option->getId()];
}
if (empty($optionsIds)) {
return;
}
$updateData = [
[
'id' => $productEntity->getId(),
'options' => $optionsIds
]
];
$this->productRepository->upsert($updateData, Context::createDefaultContext());
$this->addConfiguratorSettings($productEntity);
}
private function addConfiguratorSettings(ProductEntity $productEntity)
{
$groupOptions = [];
$criteria = (new Criteria())->addFilter(
new EqualsFilter('productId', $productEntity->getParentId())
);
$criteria
->addAssociation('option.group');
$settings = $this->configuratorSettingsRepository
->search($criteria, Context::createDefaultContext())
->getEntities();
$currentConfiguratorOptions = [];
/** @var ProductConfiguratorSettingEntity $configItem */
foreach ($settings as $configItem) {
$currentConfiguratorOptions[] = $configItem->getOptionId();
}
/** @var PropertyGroupOptionEntity $option */
foreach ($productEntity->getOptions() as $key => $option) {
$optionsIds[] = $key;
if (in_array($key, $currentConfiguratorOptions)) {
continue;
}
$groupOptions[] =
[
'optionId' => $key,
'productId' => $productEntity->getParentId()
];
}
if (!empty($groupOptions)) {
$this->configuratorSettingsRepository->upsert($groupOptions, Context::createDefaultContext());
}
}
}