custom/plugins/FourtwosixShippingCountryOnPDP/src/Subscriber/ProductDetailPageSubscriber.php line 37

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FourtwosixShippingCountryOnPDP\Subscriber;
  3. use Shopware\Core\System\Country\SalesChannel\CachedCountryRoute;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Shopware\Core\System\Country\CountryCollection;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. class ProductDetailPageSubscriber implements EventSubscriberInterface
  14. {
  15.     private CachedCountryRoute $countryRoute;
  16.     private EntityRepositoryInterface $shippingMethodPriceRepo;
  17.     /**
  18.      * @internal
  19.      */
  20.     public function __construct(CachedCountryRoute $countryRouteEntityRepositoryInterface $shippingMethodPriceRepo) {
  21.         $this->countryRoute $countryRoute;
  22.         $this->shippingMethodPriceRepo $shippingMethodPriceRepo;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  27.         return [
  28.             ProductPageLoadedEvent::class => 'onProductsLoaded'
  29.         ];
  30.     }
  31.     public function onProductsLoaded(ProductPageLoadedEvent $event)
  32.     {
  33.         $salesChannelContext $event->getSalesChannelContext();
  34.         $countries $this->getCountries($salesChannelContext);
  35.         $page $event->getPage();
  36.         $costs $this->getShippingCost($event->getContext(), $event->getSalesChannelContext());
  37.         $extensions $page->getExtensions();
  38.         $extensions['delivery_countries'] = $countries;
  39.         $extensions['shipping']['freeShippingBorder'] = $costs['maxLimit'] ?? 0;
  40.         $extensions['shipping']['shippingCosts'] = $costs['deliveryCosts'] ?? 0;
  41.         $page->setExtensions($extensions);
  42.     }
  43.     private function getCountries(SalesChannelContext $salesChannelContext): CountryCollection
  44.     {
  45.         $countries $this->countryRoute->load(new Request(), new Criteria(), $salesChannelContext)->getCountries();
  46.         $countries->sortByPositionAndName();
  47.         return $countries;
  48.     }
  49.     private function getShippingCost(Context $contextSalesChannelContext $salesChannelContext)
  50.     {
  51.         $shippingMethod $salesChannelContext->getShippingMethod();
  52.         $criteria = new Criteria();
  53.         $criteria->addFilter(new EqualsFilter('shippingMethodId'$shippingMethod->getId()));
  54.         $shippingMethodPrices $this->shippingMethodPriceRepo->search($criteria$context)->getEntities();
  55.         $prices $shippingMethodPrices->filter(
  56.             function ($price) use ($salesChannelContext) {
  57.                 if($price->getRuleId() == null){
  58.                     return false;
  59.                 }
  60.                 return in_array($price->getRuleId(), $salesChannelContext->getRuleIds(), true);
  61.             }
  62.         );
  63.         $results = array();
  64.         foreach ($prices as $price) {
  65.             if($price->getQuantityEnd() > 1){
  66.                 $results['maxLimit'] = $price->getQuantityEnd();
  67.                 $results['deliveryCosts'] = $price->getCurrencyPrice()->first()->getGross(); 
  68.             }
  69.         }
  70.         return $results;
  71.     }
  72. }