vendor/store.shopware.com/wexoshipping/src/Subscriber/Offcanvas.php line 56

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wexo\Shipping\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
  4. use Shopware\Core\Checkout\Shipping\SalesChannel\AbstractShippingMethodRoute;
  5. use Shopware\Core\Checkout\Shipping\ShippingMethodCollection;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  8. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Wexo\Shipping\Service\ShippingPriceCalculator;
  12. /**
  13.  * Class Offcanvas
  14.  * @package Wexo\Shipping\Subscriber
  15.  */
  16. class Offcanvas implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var ShippingPriceCalculator
  20.      */
  21.     private $shippingPriceCalculator;
  22.     /**
  23.      * @var AbstractShippingMethodRoute
  24.      */
  25.     private $shippingMethodRoute;
  26.     /**
  27.      * Offcanvas constructor.
  28.      * @param ShippingPriceCalculator $shippingPriceCalculator
  29.      * @param AbstractShippingMethodRoute $shippingMethodRoute
  30.      */
  31.     public function __construct(
  32.         ShippingPriceCalculator $shippingPriceCalculator,
  33.         AbstractShippingMethodRoute $shippingMethodRoute
  34.     ) {
  35.         $this->shippingPriceCalculator $shippingPriceCalculator;
  36.         $this->shippingMethodRoute $shippingMethodRoute;
  37.     }
  38.     /**
  39.      * @return string[]
  40.      */
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             OffcanvasCartPageLoadedEvent::class => 'addShippingMethodPrices',
  45.         ];
  46.     }
  47.     /**
  48.      * @param OffcanvasCartPageLoadedEvent $event
  49.      */
  50.     public function addShippingMethodPrices(OffcanvasCartPageLoadedEvent $event)
  51.     {
  52.         $cartErrors $event->getPage()->getCart()->getErrors();
  53.         $event->getPage()->getCart()->setErrors(new ErrorCollection);
  54.         $shippingMethods $this->getShippingMethods($event->getSalesChannelContext());
  55.         $clonedCart = clone $event->getPage()->getCart();
  56.         foreach ($shippingMethods as $shippingMethod) {
  57.             /* Clear all delivery before the calculation process can start */
  58.             $shippingCosts $this->shippingPriceCalculator->getShippingCostsForCart(
  59.                 $clonedCart,
  60.                 $shippingMethod,
  61.                 $this->shippingPriceCalculator
  62.                     ->createSalesChannelContextWithShippingMethod($event->getSalesChannelContext(), $shippingMethod)
  63.             );
  64.             if ($shippingCosts) {
  65.                 $pageShippingMethod $event->getPage()->getShippingMethods()->get($shippingMethod->getId());
  66.                 if ($pageShippingMethod) {
  67.                     $pageShippingMethod->addExtension('calculatedPrice'$shippingCosts);
  68.                 }
  69.             }
  70.         }
  71.         $event->getPage()->getCart()->setErrors($cartErrors);
  72.     }
  73.     /**
  74.      * @param SalesChannelContext $salesChannelContext
  75.      * @return ShippingMethodCollection
  76.      */
  77.     private function getShippingMethods(SalesChannelContext $salesChannelContext): ShippingMethodCollection
  78.     {
  79.         $request = new Request();
  80.         $request->query->set('onlyAvailable'true);
  81.         return $this->shippingMethodRoute
  82.             ->load($request$salesChannelContext, (new Criteria())->addAssociation('prices'))
  83.             ->getShippingMethods();
  84.     }
  85. }