vendor/store.shopware.com/dreiscset/src/Subscriber/ProductEvents/ProductListingResultSubscriber.php line 60

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DreiscSet\Subscriber\ProductEvents;
  3. use DreiscSet\Core\Content\DreiscSetProductExtension\DreiscSetProductExtensionConfigurationStruct;
  4. use DreiscSet\Core\Content\DreiscSetProductExtension\DreiscSetProductExtensionEntity;
  5. use DreiscSet\Core\Content\Product\SalesChannelProductRepository;
  6. use DreiscSet\Core\PriceModifier\AbstractPriceModifierService;
  7. use DreiscSet\Core\Set\AbstractSetLoaderService;
  8. use DreiscSet\Core\Set\Struct\SetGroupItemConfiguration\PriceModificationStruct;
  9. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  10. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\Struct\ArrayStruct;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class ProductListingResultSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var AbstractSetLoaderService
  18.      */
  19.     protected $setLoaderService;
  20.     /**
  21.      * @var SalesChannelProductRepository
  22.      */
  23.     protected $salesChannelProductRepository;
  24.     /**
  25.      * @var AbstractPriceModifierService
  26.      */
  27.     protected $priceModifierService;
  28.     /**
  29.      * @param AbstractSetLoaderService $setLoaderService
  30.      * @param SalesChannelProductRepository $salesChannelProductRepository
  31.      * @param AbstractPriceModifierService $priceModifierService
  32.      */
  33.     public function __construct(AbstractSetLoaderService $setLoaderServiceSalesChannelProductRepository $salesChannelProductRepositoryAbstractPriceModifierService $priceModifierService)
  34.     {
  35.         $this->setLoaderService $setLoaderService;
  36.         $this->salesChannelProductRepository $salesChannelProductRepository;
  37.         $this->priceModifierService $priceModifierService;
  38.     }
  39.     /**
  40.      * @return array|void
  41.      */
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             ProductListingResultEvent::class => 'onProductListingResultEvent'
  46.         ];
  47.     }
  48.     /**
  49.      * @param ProductListingResultEvent $salesChannelEntityLoadedEvent
  50.      * @return void
  51.      */
  52.     public function onProductListingResultEvent(ProductListingResultEvent $productListingResultEvent): void
  53.     {
  54.         $salesChannelProductEntities $productListingResultEvent->getResult()->getEntities();
  55.         $salesChannelContext $productListingResultEvent->getSalesChannelContext();
  56.         /** Iterate all products */
  57.         /** @var SalesChannelProductEntity $salesChannelProductEntity */
  58.         foreach($salesChannelProductEntities as $salesChannelProductEntity) {
  59.             /** @var DreiscSetProductExtensionEntity $dreiscSetProductExtension */
  60.             $dreiscSetProductExtension $salesChannelProductEntity->getExtension('dreiscSetProductExtension');
  61.             /** Load the parent in case of variant */
  62.             if (null === $dreiscSetProductExtension && null !== $salesChannelProductEntity->getParentId()) {
  63.                 $parentProductEntity $this->salesChannelProductRepository->get(
  64.                     $salesChannelProductEntity->getParentId(),
  65.                     new Criteria(),
  66.                     $salesChannelContext
  67.                 );
  68.                 if (null !== $parentProductEntity) {
  69.                     $dreiscSetProductExtension $parentProductEntity->getExtension('dreiscSetProductExtension');
  70.                 }
  71.             }
  72.             /** Abort, if it´s not a set */
  73.             if (empty($dreiscSetProductExtension) || empty($dreiscSetProductExtension->getDreiscSetId())) {
  74.                 continue;
  75.             }
  76.             /** Fetch the set product configuration */
  77.             $dreiscSetProductConfiguration = new DreiscSetProductExtensionConfigurationStruct($dreiscSetProductExtension->getConfiguration());
  78.             /** Listing price modification */
  79.             $priceModificationStruct null;
  80.             $dreiscSetListingPriceDisplayConfig = [];
  81.             switch ($dreiscSetProductConfiguration->getListingPriceMode()) {
  82.                 case DreiscSetProductExtensionConfigurationStruct::LISTING_PRICE_MODE__OWN_WITH_FROM:
  83.                 case DreiscSetProductExtensionConfigurationStruct::LISTING_PRICE_MODE__OWN:
  84.                     if(null !== $dreiscSetProductConfiguration->getListingPrice()) {
  85.                         $priceModificationStruct = new PriceModificationStruct();
  86.                         $priceModificationStruct->setType(PriceModificationStruct::TYPE__OWN_PRICE);
  87.                         $priceModificationStruct->setPriceInfo([
  88.                             'net' => $dreiscSetProductConfiguration->getListingPrice()->getNet(),
  89.                             'gross' => $dreiscSetProductConfiguration->getListingPrice()->getGross(),
  90.                             'linked' => true
  91.                         ]);
  92.                     }
  93.                     break;
  94.                 case DreiscSetProductExtensionConfigurationStruct::LISTING_PRICE_MODE__NO_PRICE:
  95.                     $dreiscSetListingPriceDisplayConfig['hidePrice'] = true;
  96.                     break;
  97.             }
  98.             if (DreiscSetProductExtensionConfigurationStruct::LISTING_PRICE_MODE__OWN_WITH_FROM === $dreiscSetProductConfiguration->getListingPriceMode()) {
  99.                 $dreiscSetListingPriceDisplayConfig['addFromString'] = true;
  100.             }
  101.             if (null !== $priceModificationStruct) {
  102.                 /** Recalculate product price */
  103.                 $this->priceModifierService->modifyProductPrice($salesChannelProductEntity1$priceModificationStruct$salesChannelContext);
  104.             }
  105.             if(!empty($dreiscSetListingPriceDisplayConfig)) {
  106.                 $salesChannelProductEntity->addExtension('dreiscSetListingPriceDisplayConfig', new ArrayStruct($dreiscSetListingPriceDisplayConfig));
  107.             }
  108.         }
  109.     }
  110. }