custom/static-plugins/GlobusSW6/src/Subscriber/ShippingCostsSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. namespace GlobusSW6\Subscriber;
  3. use GlobusSW6\Core\IaneoDefaults;
  4. use GlobusSW6\Service\IaneoBusinessExtension;
  5. use GlobusSW6\Service\ShippingCostsService;
  6. use Psr\Log\LoggerInterface;
  7. use Shopware\Core\Content\Product\ProductCollection;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class ShippingCostsSubscriber implements EventSubscriberInterface
  11. {
  12.     /** @var ShippingCostsService */
  13.     private $shippingCostsService;
  14.     /** @var IaneoBusinessExtension */
  15.     private $ianeoBusinessExtensionService;
  16.     /** @var LoggerInterface */
  17.     private $logger;
  18.     /**
  19.      * @param ShippingCostsService $shippingCostsService
  20.      * @param IaneoBusinessExtension $ianeoBusinessExtensionService
  21.      * @param LoggerInterface $logger
  22.      */
  23.     public function __construct(ShippingCostsService $shippingCostsServiceIaneoBusinessExtension $ianeoBusinessExtensionServiceLoggerInterface $logger)
  24.     {
  25.         $this->shippingCostsService $shippingCostsService;
  26.         $this->ianeoBusinessExtensionService $ianeoBusinessExtensionService;
  27.         $this->logger $logger;
  28.     }
  29.     public static function getSubscribedEvents()
  30.     {
  31.         return [
  32.             'product.search.result.loaded' => 'onProductLoaded'
  33.         ];
  34.     }
  35.     public function onProductLoaded(EntitySearchResultLoadedEvent $event){
  36.         /** @var ProductCollection $products */
  37.         $products $event->getResult()->getEntities();
  38.         try {
  39.             $salesChannelId $event->getContext()->getSource()->getSalesChannelId();
  40.         } catch (\Throwable $t ) {
  41.             $salesChannelId IaneoDefaults::SALES_CHANNEL_BAUMARKT// TODO-NGS: find better solution, this is only hotfix
  42.             $this->logger->info('Unable to access salesChannelId - choose default SalesChanelId Baumarkt: ' $t->getMessage());
  43.         }
  44.         if($products === null){
  45.             return;
  46.         }
  47.         if($event->getName() !== 'product.search.result.loaded'){
  48.             return;
  49.         }
  50.         // Collect shipping costs
  51.         $shippingCosts $this->shippingCostsService->checkShippingCosts($products$salesChannelId);
  52.         // add shipping costs flags to product extension
  53.         $this->ianeoBusinessExtensionService->addAttributes($products$shippingCosts'shippingCosts');
  54.     }
  55. }