<?php declare(strict_types=1);
namespace DreiscSet\Subscriber\ProductEvents;
use DreiscSet\Core\Content\DreiscSetProductExtension\DreiscSetProductExtensionConfigurationStruct;
use DreiscSet\Core\Content\DreiscSetProductExtension\DreiscSetProductExtensionEntity;
use DreiscSet\Core\Content\Product\SalesChannelProductRepository;
use DreiscSet\Core\PriceModifier\AbstractPriceModifierService;
use DreiscSet\Core\Set\AbstractSetLoaderService;
use DreiscSet\Core\Set\Struct\SetGroupItemConfiguration\PriceModificationStruct;
use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductListingResultSubscriber implements EventSubscriberInterface
{
/**
* @var AbstractSetLoaderService
*/
protected $setLoaderService;
/**
* @var SalesChannelProductRepository
*/
protected $salesChannelProductRepository;
/**
* @var AbstractPriceModifierService
*/
protected $priceModifierService;
/**
* @param AbstractSetLoaderService $setLoaderService
* @param SalesChannelProductRepository $salesChannelProductRepository
* @param AbstractPriceModifierService $priceModifierService
*/
public function __construct(AbstractSetLoaderService $setLoaderService, SalesChannelProductRepository $salesChannelProductRepository, AbstractPriceModifierService $priceModifierService)
{
$this->setLoaderService = $setLoaderService;
$this->salesChannelProductRepository = $salesChannelProductRepository;
$this->priceModifierService = $priceModifierService;
}
/**
* @return array|void
*/
public static function getSubscribedEvents()
{
return [
ProductListingResultEvent::class => 'onProductListingResultEvent'
];
}
/**
* @param ProductListingResultEvent $salesChannelEntityLoadedEvent
* @return void
*/
public function onProductListingResultEvent(ProductListingResultEvent $productListingResultEvent): void
{
$salesChannelProductEntities = $productListingResultEvent->getResult()->getEntities();
$salesChannelContext = $productListingResultEvent->getSalesChannelContext();
/** Iterate all products */
/** @var SalesChannelProductEntity $salesChannelProductEntity */
foreach($salesChannelProductEntities as $salesChannelProductEntity) {
/** @var DreiscSetProductExtensionEntity $dreiscSetProductExtension */
$dreiscSetProductExtension = $salesChannelProductEntity->getExtension('dreiscSetProductExtension');
/** Load the parent in case of variant */
if (null === $dreiscSetProductExtension && null !== $salesChannelProductEntity->getParentId()) {
$parentProductEntity = $this->salesChannelProductRepository->get(
$salesChannelProductEntity->getParentId(),
new Criteria(),
$salesChannelContext
);
if (null !== $parentProductEntity) {
$dreiscSetProductExtension = $parentProductEntity->getExtension('dreiscSetProductExtension');
}
}
/** Abort, if it´s not a set */
if (empty($dreiscSetProductExtension) || empty($dreiscSetProductExtension->getDreiscSetId())) {
continue;
}
/** Fetch the set product configuration */
$dreiscSetProductConfiguration = new DreiscSetProductExtensionConfigurationStruct($dreiscSetProductExtension->getConfiguration());
/** Listing price modification */
$priceModificationStruct = null;
$dreiscSetListingPriceDisplayConfig = [];
switch ($dreiscSetProductConfiguration->getListingPriceMode()) {
case DreiscSetProductExtensionConfigurationStruct::LISTING_PRICE_MODE__OWN_WITH_FROM:
case DreiscSetProductExtensionConfigurationStruct::LISTING_PRICE_MODE__OWN:
if(null !== $dreiscSetProductConfiguration->getListingPrice()) {
$priceModificationStruct = new PriceModificationStruct();
$priceModificationStruct->setType(PriceModificationStruct::TYPE__OWN_PRICE);
$priceModificationStruct->setPriceInfo([
'net' => $dreiscSetProductConfiguration->getListingPrice()->getNet(),
'gross' => $dreiscSetProductConfiguration->getListingPrice()->getGross(),
'linked' => true
]);
}
break;
case DreiscSetProductExtensionConfigurationStruct::LISTING_PRICE_MODE__NO_PRICE:
$dreiscSetListingPriceDisplayConfig['hidePrice'] = true;
break;
}
if (DreiscSetProductExtensionConfigurationStruct::LISTING_PRICE_MODE__OWN_WITH_FROM === $dreiscSetProductConfiguration->getListingPriceMode()) {
$dreiscSetListingPriceDisplayConfig['addFromString'] = true;
}
if (null !== $priceModificationStruct) {
/** Recalculate product price */
$this->priceModifierService->modifyProductPrice($salesChannelProductEntity, 1, $priceModificationStruct, $salesChannelContext);
}
if(!empty($dreiscSetListingPriceDisplayConfig)) {
$salesChannelProductEntity->addExtension('dreiscSetListingPriceDisplayConfig', new ArrayStruct($dreiscSetListingPriceDisplayConfig));
}
}
}
}