<?php
namespace GlobusSW6\Subscriber;
use GlobusSW6\Service\IaneoBusinessExtension;
use GlobusSW6\Service\ShippingTimeService;
use Shopware\Core\Content\Product\ProductCollection;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
class ShippingTimesSubscriber implements EventSubscriberInterface
{
/** @var ShippingTimeService */
private $shippingTimeService;
/** @var IaneoBusinessExtension */
private $ianeoBusinessExtensionService;
/**
* ShippingTimesDetailPage constructor.
* @param ShippingTimeService $shippingTimeService
* @param IaneoBusinessExtension $ianeoBusinessExtensionService
*/
public function __construct(ShippingTimeService $shippingTimeService, IaneoBusinessExtension $ianeoBusinessExtensionService)
{
$this->shippingTimeService = $shippingTimeService;
$this->ianeoBusinessExtensionService = $ianeoBusinessExtensionService;
}
public static function getSubscribedEvents()
{
return [
'product.search.result.loaded' => 'onProductLoaded'
];
}
public function onProductLoaded(EntitySearchResultLoadedEvent $event){
/** @var ProductCollection $products */
$products = $event->getResult()->getEntities();
if($products === null){
return;
}
if($event->getName() !== 'product.search.result.loaded'){
return;
}
// collect shipping times for each product
$shippingTimes = $this->shippingTimeService->checkShippingTime($products);
// add shipping time flags to product extension
$this->ianeoBusinessExtensionService->addAttributes($products, $shippingTimes, 'shippingTimes');
}
}