custom/plugins/b2bsellerscore/src/Components/Checkout/Subscriber/CheckoutSubscriber.php line 64

Open in your IDE?
  1. <?php
  2. namespace B2bSellersCore\Components\Checkout\Subscriber;
  3. use B2bSellersCore\Components\B2bPlatform\Traits\B2bContextTrait;
  4. use B2bSellersCore\Components\Checkout\Service\OrderConfirmationRecipientsService;
  5. use B2bSellersCore\Components\Checkout\Service\OrderConfirmationRecipientsServiceInterface;
  6. use B2bSellersCore\Components\Employee\Aggregate\EmployeeCustomer\EmployeeCustomerCollection;
  7. use B2bSellersCore\Components\PaymentCondition\Calculator\PaymentConditionCalculatorInterface;
  8. use Exception;
  9. use Shopware\Core\Checkout\Cart\Cart;
  10. use Shopware\Core\Checkout\Customer\CustomerEntity;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\Struct\ArrayStruct;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Shopware\Core\System\SystemConfig\SystemConfigService;
  17. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  18. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  19. use Shopware\Storefront\Page\PageLoadedEvent;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. class CheckoutSubscriber implements EventSubscriberInterface
  22. {
  23.     use B2bContextTrait;
  24.     private EntityRepositoryInterface $employeeCustomerRepository;
  25.     private PaymentConditionCalculatorInterface $paymentConditionCalculator;
  26.     private SystemConfigService $configService;
  27.     private OrderConfirmationRecipientsServiceInterface $orderConfirmationRecipientsService;
  28.     private array $config;
  29.     public function __construct(
  30.         EntityRepositoryInterface           $employeeCustomerRepository,
  31.         PaymentConditionCalculatorInterface $paymentConditionCalculator,
  32.         SystemConfigService                 $configService,
  33.         OrderConfirmationRecipientsServiceInterface $orderConfirmationRecipientsService
  34.     )
  35.     {
  36.         $this->employeeCustomerRepository $employeeCustomerRepository;
  37.         $this->paymentConditionCalculator $paymentConditionCalculator;
  38.         $this->configService $configService;
  39.         $this->orderConfirmationRecipientsService $orderConfirmationRecipientsService;
  40.         $this->config $this->configService->get('B2bSellersCore.config');
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             CheckoutConfirmPageLoadedEvent::class => [
  46.                 ['addEmployees'],
  47.                 ['addPaymentConditionToConfirmPage'],
  48.                 ['addOrderConfirmationRecipients']
  49.             ],
  50.             CheckoutCartPageLoadedEvent::class => [
  51.                 ['addEmployees'],
  52.                 ['addPaymentConditionToCartPage']
  53.             ]
  54.         ];
  55.     }
  56.     public function addPaymentConditionToConfirmPage(CheckoutConfirmPageLoadedEvent $event)
  57.     {
  58.         $this->addCalculatedPaymentCondition($event->getPage()->getCart(), $event->getSalesChannelContext());
  59.     }
  60.     public function addPaymentConditionToCartPage(CheckoutCartPageLoadedEvent $event)
  61.     {
  62.         $this->addCalculatedPaymentCondition($event->getPage()->getCart(), $event->getSalesChannelContext());
  63.     }
  64.     private function addCalculatedPaymentCondition(Cart $cartSalesChannelContext $salesChannelContext)
  65.     {
  66.         if (empty($salesChannelContext->getCustomer())) {
  67.             return;
  68.         }
  69.         $calculatedPaymentCondition $this->paymentConditionCalculator->build(
  70.             $salesChannelContext->getCustomer(),
  71.             $cart->getPrice()->getTotalPrice(),
  72.             $salesChannelContext
  73.         );
  74.         if (empty($calculatedPaymentCondition)) {
  75.             return;
  76.         }
  77.         $cart->addExtension('calculatedPaymentCondition'$calculatedPaymentCondition);
  78.     }
  79.     public function addEmployees(PageLoadedEvent $event)
  80.     {
  81.         $salesChannelContext $event->getSalesChannelContext();
  82.         /** @var CustomerEntity $customerEntity */
  83.         $customerEntity $salesChannelContext->getCustomer();
  84.         if (empty($customerEntity)) {
  85.             $event->getPage()->addExtension('employees', (new EmployeeCustomerCollection([]))->getEmployees());
  86.             return;
  87.         }
  88.         $employeeCustomers $this->getEmployees($customerEntity->getId(), $salesChannelContext);
  89.         $event->getPage()->addExtension('employees'$employeeCustomers->getEmployees());
  90.     }
  91.     /**
  92.      * @throws Exception
  93.      */
  94.     public function addOrderConfirmationRecipients(CheckoutConfirmPageLoadedEvent $event)
  95.     {
  96.         if (!$this->hasB2bPlatformContext($event->getSalesChannelContext())) {
  97.             return;
  98.         }
  99.         if (!isset($this->config['enableOrderMailRecipientSelection']) || !$this->config['enableOrderMailRecipientSelection']) {
  100.             return;
  101.         }
  102.         try {
  103.             $orderMailRecipientOptions $this->orderConfirmationRecipientsService->getRecipients($event->getSalesChannelContext());
  104.             $orderMailRecipients $this->orderConfirmationRecipientsService->getSelectedRecipients($event->getSalesChannelContext(), $event->getRequest());
  105.            if(!empty($orderMailRecipientOptions)) {
  106.                $event->getPage()->addExtension('orderMailRecipientOptions', new ArrayStruct($orderMailRecipientOptions));
  107.                $event->getPage()->addExtension('orderMailRecipients', new ArrayStruct($orderMailRecipients));
  108.            }
  109.         } catch (Exception $exception) {
  110.             throw new Exception('Something went wrong. Recipients could not be added to page');
  111.         }
  112.     }
  113.     private function getEmployees(string $customerIdSalesChannelContext $context): ?EmployeeCustomerCollection
  114.     {
  115.         $criteria = new Criteria();
  116.         $criteria->addFilter(new EqualsFilter('customerId'$customerId));
  117.         $criteria->addAssociation('employee');
  118.         return $this->employeeCustomerRepository->search($criteria$context->getContext())->getEntities();
  119.     }
  120. }