<?php declare(strict_types=1);
/**
* @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
*
* @author Jakub MedyĆski <jme@crehler.com>
* @support Crehler <support@crehler.com>
* @created 12 lut 2020
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\VariantsAdvanced\Subscriber;
use Crehler\MojeBambinoEngine\Service\CrehlerContextService;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionCollection;
use Shopware\Core\Content\Property\PropertyEvents;
use Shopware\Core\Content\Property\PropertyGroupEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\CustomField\CustomFieldEvents;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Page\Product\Configurator\ProductPageConfiguratorLoader;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductListingResultSubscriber implements EventSubscriberInterface
{
private ProductPageConfiguratorLoader $pageConfiguratorLoader;
private CrehlerContextService $crehlerContextService;
private SalesChannelRepositoryInterface $salesChannelProductRepository;
public function __construct(ProductPageConfiguratorLoader $pageConfiguratorLoader, CrehlerContextService $crehlerContextService, SalesChannelRepositoryInterface $salesChannelProductRepository)
{
$this->pageConfiguratorLoader = $pageConfiguratorLoader;
$this->crehlerContextService = $crehlerContextService;
$this->salesChannelProductRepository = $salesChannelProductRepository;
}
public static function getSubscribedEvents(): array
{
return [
'sales_channel.product.loaded' => 'handleProductResult',
];
}
public function handleProductResult(SalesChannelEntityLoadedEvent $event): void
{
if ($this->crehlerContextService->get(CrehlerContextService::SITEMAP_GENERATE) || $this->crehlerContextService->get(CrehlerContextService::PRODUCT_EXPORT_GENERATE)) return;
$products = $event->getEntities();
$this->addExtensionToProducts($products, $event->getSalesChannelContext());
}
private function addExtensionToProducts(array $products, SalesChannelContext $salesChannelContext): void
{
/** @var SalesChannelProductEntity $product */
foreach ($products as $product) {
if ($product->getParentId() === null && $product->getMainVariantId()) {
$productVariant = $this->salesChannelProductRepository->search(new Criteria([$product->getMainVariantId()]), $salesChannelContext)->first();
$product->addExtensions(
[
'configuratorSettings' => $this->pageConfiguratorLoader->load($productVariant, $salesChannelContext),
'mainVariantProduct'=> $productVariant,
]
);
} else {
$product->addExtensions(
[
'configuratorSettings' => $this->pageConfiguratorLoader->load($product, $salesChannelContext),
]
);
}
}
}
}