custom/static-plugins/GlobusSW6/src/Subscriber/CalculatePickupTimeSubscriber.php line 81

Open in your IDE?
  1. <?php
  2. namespace GlobusSW6\Subscriber;
  3. use GlobusSW6\Service\ShippingTimeService;
  4. use GlobusSW6\Service\StoreLocator\PickupTimeCalculator;
  5. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  6. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use GlobusSW6\Core\IaneoDefaults;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use GlobusSW6\Service\StoreLocator\Struct\PickupTimeStruct;
  11. class CalculatePickupTimeSubscriber implements EventSubscriberInterface
  12. {
  13.     /** @var PickupTimeCalculator */
  14.     private $pickupTimeCalculator;
  15.     /** @var SessionInterface */
  16.     private $session;
  17.     /**
  18.      * ShippingTimesDetailPage constructor.
  19.      * @param ShippingTimeService $shippingTimeService
  20.      */
  21.     public function __construct(PickupTimeCalculator $pickupTimeCalculatorSessionInterface $session)
  22.     {
  23.         $this->pickupTimeCalculator $pickupTimeCalculator;
  24.         $this->session $session;
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.              return [
  29.                  CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded',
  30.                  CartConvertedEvent::class => 'onCartConverted'
  31.              ];
  32.     }
  33.     public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event)
  34.     {
  35.         //TODO SMN Ist das sicher?
  36.         $storeId $this->session->get('ianeoCurrentStore');
  37.         if (empty($storeId))
  38.         {
  39.             return;
  40.         }
  41.         $holzzuschnitt false;
  42.         if(!empty($event->getPage()->getCart()->getExtension("acid_holzzuschnitt")))
  43.         {
  44.             $holzzuschnitt true;
  45.         }
  46.         $pickuptime $this->pickupTimeCalculator->calculate(
  47.             $storeId,
  48.             IaneoDefaults::PICKUPTIME,
  49.             NULL,
  50.             false,
  51.             $holzzuschnitt,
  52.             $event->getSalesChannelContext()->getContext()
  53.         );
  54.         $event->getPage()->getCart()->removeExtension("ianeo_pickup_time");
  55.         $cart $event->getPage()->getCart();
  56.         if($pickuptime) {
  57.             $cart->addExtension(
  58.                 "ianeo_pickup_time",new PickupTimeStruct($pickuptime->format('d.m.Y H:i'))
  59.             );
  60.         } else {
  61.             // Es konnte keine Abholzeit für den Store ermittelt werden
  62.             $cart->addExtension("ianeo_pickup_time"null);
  63.         }
  64.         $event->getPage()->setCart($cart);
  65.     }
  66.     public function onCartConverted(CartConvertedEvent $event)
  67.     {
  68.         $convertedCart $event->getConvertedCart();
  69.         //TODO SMN Ist das sicher?
  70.         $storeId $this->session->get('ianeoCurrentStore');
  71.         if (empty($storeId))
  72.         {
  73.             return;
  74.         }
  75.         $holzzuschnitt false;
  76.         if(!empty($event->getCart()->getExtension("acid_holzzuschnitt")))
  77.         {
  78.             $holzzuschnitt true;
  79.         }
  80.         $pickuptime $this->pickupTimeCalculator->calculate(
  81.             $storeId,
  82.             IaneoDefaults::PICKUPTIME,
  83.             NULL,
  84.             false,
  85.             $holzzuschnitt,
  86.             $event->getSalesChannelContext()->getContext()
  87.         );
  88.         if(!empty($convertedCart))
  89.         {
  90.             if($pickuptime) {
  91.                 $convertedCart['ianeoAttributes'] = [
  92.                     'pickupDatetime' => $pickuptime->format('d.m.Y H:i')
  93.                 ];
  94.             }
  95.         }
  96.         $event->setConvertedCart($convertedCart);
  97.     }
  98. }