custom/static-plugins/GlobusSW6/src/Subscriber/DepositSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. namespace GlobusSW6\Subscriber;
  3. use GlobusSW6\Service\Deposit\DepositService;
  4. use Shopware\Core\Content\Product\ProductCollection;
  5. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use GlobusSW6\Service\IaneoBusinessExtension;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  9. class DepositSubscriber implements EventSubscriberInterface
  10. {
  11.     /** @var IaneoBusinessExtension */
  12.     private $ianeoBusinessExtension;
  13.     /** @var DepositService */
  14.     private $depositService;
  15.     /**
  16.      * @param IaneoBusinessExtension $ianeoBusinessExtension
  17.      * @param DepositService $depositService
  18.      */
  19.     public function __construct(IaneoBusinessExtension $ianeoBusinessExtensionDepositService $depositService)
  20.     {
  21.         $this->ianeoBusinessExtension $ianeoBusinessExtension;
  22.         $this->depositService $depositService;
  23.     }
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             'product.search.result.loaded' => 'onProductLoaded',
  28.         ];
  29.     }
  30.     // Check if product contains deposits and add the deposits into extension IaneoExtensions
  31.     public function onProductLoaded(EntitySearchResultLoadedEvent $event)
  32.     {
  33.         // do not fire event in backend context
  34.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  35.             if ($event->getContext()->getSource()->isAdmin()) {
  36.                 return;
  37.             }
  38.         }
  39.         /** @var ProductCollection $products */
  40.         $products $event->getResult()->getEntities();
  41.         if (is_null($products)){
  42.             return;
  43.         }
  44.         $deposits $this->depositService->collectDeposits($products);
  45.         
  46.         // Add deposits into extension
  47.         $this->ianeoBusinessExtension->addAttributes($event->getResult()->getEntities(), $deposits'deposits');
  48.     }
  49. }