custom/static-plugins/GlobusSW6/src/Subscriber/WarrantySubscriber.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace GlobusSW6\Subscriber;
  3. use GlobusSW6\Core\Content\Warranty\WarrantyEntity;
  4. use GlobusSW6\Service\IaneoBusinessExtension;
  5. use GlobusSW6\Service\WarrantyService;
  6. use Shopware\Core\Content\Product\ProductEntity;
  7. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  8. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  9. use Shopware\Core\Framework\Api\Context\SystemSource;
  10. use Shopware\Core\Framework\Struct\ArrayStruct;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  13. class WarrantySubscriber implements EventSubscriberInterface
  14. {
  15.     /** @var WarrantyService */
  16.     private $warrantyService;
  17.     /** @var IaneoBusinessExtension */
  18.     private $ianeoBusinessExtension;
  19.     /**
  20.      * WarrantySubscriber constructor.
  21.      * @param WarrantyService $warrantyService
  22.      * @param IaneoBusinessExtension $ianeoBusinessExtension
  23.      */
  24.     public function __construct(WarrantyService $warrantyServiceIaneoBusinessExtension $ianeoBusinessExtension)
  25.     {
  26.         $this->warrantyService $warrantyService;
  27.         $this->ianeoBusinessExtension $ianeoBusinessExtension;
  28.     }
  29.     public static function getSubscribedEvents()
  30.     {
  31.         return [
  32.             'product.search.result.loaded' => ['onProduct'1000]
  33.         ];
  34.     }
  35.     public function onProduct(EntitySearchResultLoadedEvent $event)
  36.     {
  37.         // do not fire event in backend context
  38.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  39.             if ($event->getContext()->getSource()->isAdmin()) {
  40.                 return;
  41.             }
  42.         }
  43.         if ($event->getContext()->getSource() instanceof SystemSource) {
  44.             return;
  45.         }
  46.         $products $event->getResult()->getElements();
  47.         if (empty($products)) {
  48.             return;
  49.         }
  50.         // detect warranties
  51.         $warranties $this->warrantyService->checkWarranty($event->getResult()->getEntities(), $event->getContext()->getSource()->getSalesChannelId());
  52.         foreach ($products as $product) {
  53.             /** @var ProductEntity $product */
  54.             /** @var array $productExtensions */
  55.             $productExtensions $product->getExtensions();
  56.             if (!array_key_exists('ianeoExtensions'$productExtensions)) {
  57.                 continue;
  58.             }
  59.             /** @var ArrayStruct $ianeoExtensions */
  60.             $ianeoExtensions $productExtensions['ianeoExtensions'];
  61.             // in case product has warranties, remove potential additionalServiceSets
  62.             if (array_key_exists($product->getId(), $warranties)) {
  63.                 if ($ianeoExtensions->has('additionalServiceSets') && ($ianeoExtensions['additionalServiceSets'] !== null)) {
  64.                     unset($ianeoExtensions['additionalServiceSets']);
  65.                 }
  66.             }
  67.         }
  68.         // add warranties to products
  69.         $this->ianeoBusinessExtension->addAttributes($event->getResult()->getEntities(), $warranties'warranty');
  70.     }
  71. }