custom/plugins/CompraEnvironmentalDiscountSW6/src/Storefront/Subscriber/FrontendSubscriber.php line 61

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Compra\EnvironmentalDiscountSW6\Storefront\Subscriber;
  3. use Compra\EnvironmentalDiscountSW6\Core\System\EnvironmentalDiscountService;
  4. use Compra\InvoiceDispatchSW6\Core\System\InvoiceDispatchService;
  5. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  6. use Shopware\Core\Checkout\Order\OrderEntity;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\Struct\ArrayEntity;
  9. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  10. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  11. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\Validator\Constraints\NotBlank;
  16. class FrontendSubscriber implements EventSubscriberInterface
  17. {
  18.     /** @var EnvironmentalDiscountService  */
  19.     protected $environmentalDiscountService;
  20.     /** @var InvoiceDispatchService */
  21.     protected $invoiceDispatchService;
  22.     /** @var Request */
  23.     protected $request;
  24.     public function __construct(
  25.         EnvironmentalDiscountService $environmentalDiscountService,
  26.         InvoiceDispatchService $invoiceDispatchService,
  27.         RequestStack $requestStack
  28.     )
  29.     {
  30.         $this->environmentalDiscountService $environmentalDiscountService;
  31.         $this->invoiceDispatchService $invoiceDispatchService;
  32.         $this->request $requestStack->getCurrentRequest();
  33.     }
  34.     /**
  35.      * @return string[]
  36.      */
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirm',
  41.             'framework.validation.order.create' => 'onBuildValidation',
  42.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishLoaded',
  43.             CheckoutOrderPlacedEvent::class => 'onOrderPlacedEvent'
  44.         ];
  45.     }
  46.     /**
  47.      * Helper function to add validation fields for order create validation.
  48.      * Add constraint notBlank on invoice mail address if 'mail' is selected
  49.      *
  50.      * @param BuildValidationEvent $event
  51.      */
  52.     public function onBuildValidation(BuildValidationEvent $event): void
  53.     {
  54.         if ($event->getDefinition()->getName() !== 'order.create') {
  55.             return;
  56.         }
  57.         // add constraint notBlank on invoice mail address if 'mail' is selected
  58.         if ($this->request->get('environmental_discount') === InvoiceDispatchService::INVOICE_DISPATCH_MAIL) {
  59.             $event->getDefinition()->add('invoice_mail_address', new NotBlank());
  60.         }
  61.     }
  62.     /**
  63.      * Subscribes the CheckoutConfirmPageLoadedEvent to set current invoice dispatch mode/mail + template vars.
  64.      * Also checks if should display the environmental discount panel.
  65.      * @param CheckoutConfirmPageLoadedEvent $event
  66.      */
  67.     public function onCheckoutConfirm(CheckoutConfirmPageLoadedEvent $event)
  68.     {
  69.         $salesChannelContext $event->getSalesChannelContext();
  70.         $customer $event->getSalesChannelContext()->getCustomer();
  71.         if (!$customer) {
  72.             return;
  73.         }
  74.         $customFields $customer->getCustomFields();
  75.         // get all payment IDs for which the environmental discount is allowed
  76.         $invoicePayments $this->environmentalDiscountService->getPluginConfig('invoicePayment');
  77.         // get the configured or default environmental discount amount for the display in storefront
  78.         $environmentalDiscount abs($this->environmentalDiscountService->getPluginConfig('environmentalDiscount') ?? EnvironmentalDiscountService::ENVIRONMENTAL_DISCOUT_PRICE_DEFAULT);
  79.         $currentPaymentMethod $event->getSalesChannelContext()->getPaymentMethod();
  80.         $currentPaymentMethodId $currentPaymentMethod->getId();
  81.         $customerInvoiceMailEnabled false;
  82.         $currentOption $this->environmentalDiscountService->getCurrentOption($customer);
  83.         $currentInvoiceMailAddress $this->environmentalDiscountService->getCurrentInvoiceMailAddress($customer);
  84.         $showPanel false;
  85.         if ($customFields && array_key_exists('compra_invoice_mail_enabled'$customFields)) {
  86.             $customerInvoiceMailEnabled $customFields['compra_invoice_mail_enabled'];
  87.         }
  88.         if (!$customerInvoiceMailEnabled && $invoicePayments && in_array($currentPaymentMethodId$invoicePaymentstrue)
  89.         && $currentPaymentMethod->getActive() && in_array($currentPaymentMethodId$salesChannelContext->getSalesChannel()->getPaymentMethodIds(), true)) {
  90.             /*
  91.              * Show the environmental discount panel if:
  92.              * - current customer invoice dispatch is not already 'mail' AND
  93.              * - payments for invoice/environmental discount are set AND
  94.              * - current payment method is allowed for environmental discount AND
  95.              * - current payment method is active AND
  96.              * - current payment method is allowed for the current sales channel
  97.              */
  98.             $showPanel true;
  99.         }
  100.         /*
  101.          * Set the session values.
  102.          * Because otherwise, if we only load the checkout/confirm page and customer won't select an invoice dispatch method,
  103.          * no change session value ajax call will be performed via JS and therefore no session value will be set.
  104.          */
  105.         $this->environmentalDiscountService->setCurrentOption($currentOption);
  106.         $this->environmentalDiscountService->setCurrentInvoiceMailAddress($currentInvoiceMailAddress);
  107.         $event->getPage()->addExtension('environmental_discount', new ArrayEntity([
  108.             'currentOption' => $currentOption,
  109.             'currentInvoiceMailAddress' => $currentInvoiceMailAddress,
  110.             'showPanel' => $showPanel,
  111.             'discount' => $environmentalDiscount
  112.         ]));
  113.     }
  114.     /**
  115.      * Subscribes the CheckoutFinishPageLoaded to save the invoice dispatch method and mail address after successful order.
  116.      * This is a little fallback for plugins that skip the CheckoutOrderPlacedEvent (e.g. PayPal)
  117.      *
  118.      * @param CheckoutFinishPageLoadedEvent $event
  119.      */
  120.     public function onCheckoutFinishLoaded(CheckoutFinishPageLoadedEvent $event): void
  121.     {
  122.         $order $event->getPage()->getOrder();
  123.         $this->saveInvoiceDispatchAfterOrder($order$event->getContext());
  124.         /*
  125.          * ONLY HERE:
  126.          * Unset session invoice dispatch mode after successful save of data.
  127.          * Don't already unset on CheckoutOrderPlacedEvent because some checkout plugins (e.g. PayPal) skip the CheckoutOrderPlacedEvent.
  128.          * Therefore session data could only be unset here.
  129.          */
  130.         $this->environmentalDiscountService->unsetCurrentOption();
  131.         $this->environmentalDiscountService->unsetCurrentInvoiceMailAddress();
  132.     }
  133.     /**
  134.      * Subscribes the CheckoutOrderPlacedEvent to save the invoice dispatch method and mail address after successful order.
  135.      * @param CheckoutOrderPlacedEvent $event
  136.      */
  137.     public function onOrderPlacedEvent(CheckoutOrderPlacedEvent $event): void
  138.     {
  139.         $order $event->getOrder();
  140.         $this->saveInvoiceDispatchAfterOrder($order$event->getContext());
  141.     }
  142.     /**
  143.      * Helper function to save the invoice dispatch data after successful order.
  144.      *
  145.      * @param OrderEntity $order
  146.      * @param Context $context
  147.      */
  148.     protected function saveInvoiceDispatchAfterOrder(OrderEntity $orderContext $context): void
  149.     {
  150.         $orderCustomer $order->getOrderCustomer();
  151.         if (!$orderCustomer) {
  152.             return;
  153.         }
  154.         $customer $orderCustomer->getCustomer();
  155.         if (!$customer) {
  156.             return;
  157.         }
  158.         $invoiceDispatchMode $this->environmentalDiscountService->getCurrentOption($customer);
  159.         $invoiceMailAddress $this->environmentalDiscountService->getCurrentInvoiceMailAddress($customer);
  160.         $this->invoiceDispatchService->saveInvoiceDispatch($invoiceDispatchMode$invoiceMailAddress$customer$context);
  161.     }
  162. }