custom/plugins/DtgsDeliveryDateSw6/src/Subscriber/CheckoutSubscriber.php line 200

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dtgs\DeliveryDate\Subscriber;
  3. use Dtgs\DeliveryDate\Components\Helper\CalculationHelper;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\Struct\ArrayEntity;
  8. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  9. use Shopware\Core\Framework\Validation\DataValidationDefinition;
  10. use Shopware\Core\Framework\Validation\DataValidator;
  11. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  12. use Shopware\Core\PlatformRequest;
  13. use Shopware\Core\System\Language\LanguageCollection;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  16. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPage;
  17. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. use Symfony\Component\HttpFoundation\Session\Session;
  21. use Symfony\Component\Validator\Constraints\NotBlank;
  22. use Symfony\Component\Validator\Constraints\NotNull;
  23. use Symfony\Component\Validator\ConstraintViolation;
  24. use Symfony\Component\Validator\ConstraintViolationList;
  25. use Swag\PayPal\OrdersApi\Builder\Event\PayPalV2ItemFromOrderEvent;
  26. class CheckoutSubscriber implements EventSubscriberInterface
  27. {
  28.     /**
  29.      * @var SystemConfigService
  30.      */
  31.     private $systemConfigService;
  32.     /**
  33.      * @var Session
  34.      */
  35.     private $session;
  36.     /**
  37.      * @var RequestStack
  38.      */
  39.     private $requestStack;
  40.     /**
  41.      * @var DataValidator
  42.      */
  43.     private $validator;
  44.     /**
  45.      * @var CalculationHelper
  46.      */
  47.     private $calculationHelper;
  48.     /**
  49.      * @var EntityRepositoryInterface
  50.      */
  51.     private $languageRepository;
  52.     /**
  53.      * CheckoutSubscriber constructor.
  54.      * @param SystemConfigService $systemConfigService
  55.      * @param Session $session
  56.      * @param RequestStack $requestStack
  57.      * @param DataValidator $validator
  58.      * @param CalculationHelper $calculationHelper
  59.      * @param EntityRepositoryInterface $languageRepository
  60.      */
  61.     public function __construct(SystemConfigService $systemConfigService,
  62.                                 Session $session,
  63.                                 RequestStack $requestStack,
  64.                                 DataValidator $validator,
  65.                                 CalculationHelper $calculationHelper,
  66.                                 EntityRepositoryInterface $languageRepository)
  67.     {
  68.         $this->systemConfigService $systemConfigService;
  69.         $this->session $session;
  70.         $this->requestStack $requestStack;
  71.         $this->validator $validator;
  72.         $this->calculationHelper $calculationHelper;
  73.         $this->languageRepository $languageRepository;
  74.     }
  75.     public static function getSubscribedEvents(): array
  76.     {
  77.         return [
  78.             CheckoutConfirmPageLoadedEvent::class => 'onConfirmPageLoaded',
  79.             CheckoutFinishPageLoadedEvent::class => 'onFinishPageLoaded',
  80.             'framework.validation.order.create' => 'onValidateOrder',
  81.         ];
  82.     }
  83.     /**
  84.      * Wenn das DeliveryDate ein Pflichtfeld ist, prüfen wir hier ob es gesetzt ist
  85.      *
  86.      * @param BuildValidationEvent $event
  87.      */
  88.     public function onValidateOrder($event)
  89.     {
  90.         $salesChannelId $this->requestStack->getCurrentRequest()->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
  91.         $deliveryDateConfig $this->systemConfigService->get('DtgsDeliveryDateSw6.config'$salesChannelId);
  92.         if(!isset($deliveryDateConfig['deliveryDateRequired']) || $deliveryDateConfig['deliveryDateRequired'] === false || $deliveryDateConfig['deliveryDateActive'] === false) {
  93.             return;
  94.         }
  95.         $data = [];
  96.         $data['deliverydate'] = $this->session->get('deliverydate'null);
  97.         $definition = new DataValidationDefinition('checkout.deliverydate');
  98.         $definition->add('deliverydate', new NotBlank());
  99.         $violations $this->validator->getViolations($data$definition);
  100.         if (!$violations->count()) {
  101.             return;
  102.         }
  103.         $violations = new ConstraintViolationList();
  104.         $violations->add(new ConstraintViolation(
  105.             'Delivery Date not set',
  106.             'Delivery Date not set',
  107.             [],
  108.             '',
  109.             'deliverydate',
  110.             $data['deliverydate'],
  111.             null,
  112.             'dtgs-delivery-date-null'
  113.         ));
  114.         // wirf diese Exception und du kommst zurück zur confirm seite
  115.         throw new ConstraintViolationException($violations$data);
  116.     }
  117.     /**
  118.      * Event für Confirm Seite
  119.      *
  120.      * @param CheckoutConfirmPageLoadedEvent $event
  121.      * @throws \Exception
  122.      */
  123.     public function onConfirmPageLoaded($event)
  124.     {
  125.         /** @var CheckoutConfirmPageLoadedEvent $page */
  126.         $page $event->getPage();
  127.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  128.         $deliveryDateConfig $this->systemConfigService->get('DtgsDeliveryDateSw6.config'$salesChannelId);
  129.         #var_dump($deliveryDateConfig); exit;
  130.         //since V6.0.15
  131.         $this->calculationHelper->loadConfig($deliveryDateConfig);
  132.         if(isset($deliveryDateConfig['deliveryDateTimeZone']) && $deliveryDateConfig['deliveryDateTimeZone'] != ''$this->calculationHelper->setTimezone($deliveryDateConfig['deliveryDateTimeZone']);
  133.         if(isset($deliveryDateConfig['deliveryDateMaxTime'])) $this->calculationHelper->setMaxTime($deliveryDateConfig['deliveryDateMaxTime']);
  134.         //Included days
  135.         $deliveryDateConfig['includedDays'] = json_encode($this->calculationHelper->getIncludedDays());
  136.         //Excluded days
  137.         $deliveryDateConfig['excludedDates'] = json_encode($this->calculationHelper->getExcludedDates());
  138.         if(!$deliveryDateConfig['deliveryDateUseBlockedDaysForCalculation']) {
  139.             $this->calculationHelper->setIncludedDays(array(1,2,3,4,5,6,7));
  140.             $this->calculationHelper->setExcludedDates(array());
  141.         }
  142.         //Inline Picker
  143.         $inlinePickerAsString = ($deliveryDateConfig['deliveryDateInlinePicker']) ? 'true' 'false';
  144.         $deliveryDateConfig['deliveryDateInlinePickerAsString'] = $inlinePickerAsString;
  145.         //Calendar weeks
  146.         $showWeeksAsString = ($deliveryDateConfig['deliveryDateShowWeeks']) ? 'true' 'false';
  147.         $deliveryDateConfig['deliveryDateShowWeeksAsString'] = $showWeeksAsString;
  148.         //this seems to be a bug...
  149.         if(!isset($deliveryDateConfig['deliveryDateMinDaysBefore'])) $deliveryDateConfig['deliveryDateMinDaysBefore'] = 0;
  150.         if(!isset($deliveryDateConfig['deliveryDateMaxDaysAfter'])) $deliveryDateConfig['deliveryDateMaxDaysAfter'] = 0;
  151.         //Earliest and latest date
  152.         $deliveryDateConfig['deliveryDateMinDaysBefore'] = $this->calculationHelper->calculateMinDate($deliveryDateConfig['deliveryDateMinDaysBefore']);
  153.         $deliveryDateConfig['deliveryDateMaxDaysAfter'] = $this->calculationHelper->calculateMaxDate($deliveryDateConfig['deliveryDateMaxDaysAfter']);
  154.         //Ajax URL
  155.         $deliveryDateConfig['deliveryDateAjaxUrl'] = $event->getSalesChannelContext()->getSalesChannel()->getDomains()->first()->getUrl();
  156.         //Check if there is a delivery date in session already
  157.         if($this->session->get('deliverydate'null) !== null) {
  158.             $deliveryDateConfig['deliveryDateInSession'] = $this->session->get('deliverydate'null);
  159.         }
  160.         $deliveryDateConfig['deliveryDateLocale'] = $this->getLanguageCode($event->getContext());
  161.         //Time picker
  162.         $timePickerAsString = ($deliveryDateConfig['deliveryDateEnableTimePicker']) ? 'true' 'false';
  163.         $deliveryDateConfig['deliveryDateEnableTimePickerAsString'] = $timePickerAsString;
  164.         $page->addExtension('DeliveryDateConfig', new ArrayEntity([
  165.             'data' => $deliveryDateConfig
  166.         ]));
  167.     }
  168.     /**
  169.      * Event für Finish Seite
  170.      *
  171.      * @param CheckoutFinishPageLoadedEvent $event
  172.      * @throws \Exception
  173.      */
  174.     public function onFinishPageLoaded($event)
  175.     {
  176.         /** @var CheckoutFinishPage $page */
  177.         $page $event->getPage();
  178.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  179.         $deliveryDateConfig $this->systemConfigService->get('DtgsDeliveryDateSw6.config'$salesChannelId);
  180.         $order $page->getOrder();
  181.         $customFields $order->getCustomFields();
  182.         if(isset($customFields['dtgs_delivery_date']) && $customFields['dtgs_delivery_date'] != '') {
  183.             $format = ($deliveryDateConfig['deliveryDateEnableTimePicker']) ? 'd.m.Y H:i' 'd.m.Y';
  184.             $deliveryDateAsString date($formatstrtotime($customFields['dtgs_delivery_date']));
  185.             $page->addExtension('DeliveryDateConfig', new ArrayEntity([
  186.                 'asString' => $deliveryDateAsString
  187.             ]));
  188.         }
  189.     }
  190.     private function getLanguageCode(Context $context): string
  191.     {
  192.         $language $this->getLanguage($context);
  193.         if ($language === null) {
  194.             return 'en';
  195.         }
  196.         return $language->getName();
  197.     }
  198.     private function getLanguage(Context $context)
  199.     {
  200.         $languageId $context->getLanguageId();
  201.         $criteria = new Criteria([$languageId]);
  202.         $criteria->addAssociation('locale');
  203.         /** @var LanguageCollection $languageCollection */
  204.         $languageCollection $this->languageRepository->search($criteria$context)->getEntities();
  205.         return $languageCollection->get($languageId);
  206.     }
  207. }