<?php declare(strict_types=1);
namespace GlobusSW6\Subscriber\Variants;
use GlobusSW6\Core\Content\Attributes\Product\ProductAttributesEntity;
use GlobusSW6\Service\IaneoBusinessExtension;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\Api\Context\AdminApiSource;
use Shopware\Core\Framework\Api\Context\SystemSource;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class VariantSubscriber implements EventSubscriberInterface
{
/** @var IaneoBusinessExtension */
private $ianeoBusinessExtensionService;
/**
* @param IaneoBusinessExtension $ianeoBusinessExtensionService
*/
public function __construct(IaneoBusinessExtension $ianeoBusinessExtensionService)
{
$this->ianeoBusinessExtensionService = $ianeoBusinessExtensionService;
}
public static function getSubscribedEvents(): array
{
return [
'product.search.result.loaded' => 'onProductLoaded'
];
}
public function onProductLoaded(EntitySearchResultLoadedEvent $event)
{
// do not fire event in backend context
if ($event->getContext()->getSource() instanceof AdminApiSource) {
if ($event->getContext()->getSource()->isAdmin()) {
return;
}
}
if ($event->getContext()->getSource() instanceof SystemSource) {
return;
}
$products = $event->getResult()->getEntities();
$variantInfoArray = [];
/** @var ProductEntity $product */
foreach ($products as $product) {
/** @var ProductAttributesEntity $ianeoAttributes */
$ianeoAttributes = $product->getExtension('ianeoAttributes');
if (!is_null($ianeoAttributes->getVariantsListingMode())) {
$variantInfos = new ArrayStruct();
$variantInfos->set('isListingAufgefaechert', $ianeoAttributes->getVariantsListingMode() === 'a');
$variantInfos->set('isPdpDropdown', $ianeoAttributes->getVariantsPdpPropertyMode() === 'd');
$variantInfos->set('isPdpBox', $ianeoAttributes->getVariantsPdpPropertyMode() === 'b');
$variantInfos->set('isSerienArtikel', $ianeoAttributes->getVariantsSerienArtikelOrVariant() === 's');
$variantInfoArray[$product->getId()] = $variantInfos;
} else {
$variantInfoArray[$product->getId()] = null;
}
}
$this->ianeoBusinessExtensionService->addAttributes($products, $variantInfoArray, 'variantInfos');
}
}