custom/static-plugins/CioApprovalThreshold/src/Subscriber/CheckoutConfirmSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. namespace CioApprovalThreshold\Subscriber;
  3. use CioApprovalThreshold\Definition\Customer\CustomerApprovalThresholdEntity;
  4. use CioApprovalThreshold\Definition\Order\OrderApprovalThresholdEntity;
  5. use Shopware\Core\Checkout\Customer\CustomerEntity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  11. use Shopware\Storefront\Event\StorefrontRenderEvent;
  12. use Shopware\Storefront\Page\Account\Order\AccountOrderPage;
  13. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class CheckoutConfirmSubscriber implements EventSubscriberInterface
  16. {
  17.     private EntityRepository $customerRepository;
  18.     public function __construct(EntityRepository $customerRepository)
  19.     {
  20.         $this->customerRepository $customerRepository;
  21.     }
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             StorefrontRenderEvent::class => 'onStorefrontRenderEvent'
  26.         ];
  27.     }
  28.     public function onStorefrontRenderEvent(StorefrontRenderEvent $event)
  29.     {
  30.         $possibleReleaseCustomer = [];
  31.         $defaultReleaseCustomer null;
  32.         $customer $event->getSalesChannelContext()->getCustomer();
  33.         $freigeber = [];
  34.         if ($customer instanceof CustomerEntity) {
  35.             if (is_array($event->getParameters()) && key_exists('page'$event->getParameters())) {
  36.                 $page $event->getParameters()['page'];
  37.                 if($page instanceof AccountOrderPage) {
  38.                     foreach($page->getOrders()->getElements() as $orderId => $order) {
  39.                         $freigeberCustomer null;
  40.                         /** @var OrderApprovalThresholdEntity $orderApprovalThresholdEntity */
  41.                         $orderApprovalThresholdEntity $order->getExtension('approvalThresholdExtension');
  42.                         if($orderApprovalThresholdEntity instanceof OrderApprovalThresholdEntity) {
  43.                             $freigeberId $orderApprovalThresholdEntity->getSelectedReleaseCustomerId();
  44.                             $criteria = new Criteria();
  45.                             $criteria->addFilter(new EqualsFilter('id'$freigeberId));
  46.                             $freigeberCustomer $this->customerRepository->search($criteria$event->getContext())->first();
  47.                         }
  48.                         if ($freigeberCustomer instanceof CustomerEntity) {
  49.                             $freigeber[$orderId] = $freigeberCustomer->getFirstName() . ' ' $freigeberCustomer->getLastName();
  50.                         } else {
  51.                             $freigeber[$orderId] = '';
  52.                         }
  53.                     }
  54.                 }
  55.                 if ($page instanceof CheckoutConfirmPage) {
  56.                     $totalPrice $page->getCart()->getPrice()->getTotalPrice();
  57.                     $criteria = new Criteria();
  58.                     $criteria->addFilter(new RangeFilter('approvalThresholdExtension.releaseThreshold', [
  59.                         RangeFilter::GTE => $totalPrice
  60.                     ]));
  61.                     $criteria->addSorting(new FieldSorting('lastName'FieldSorting::ASCENDING), new FieldSorting('firstName'FieldSorting::ASCENDINGtrue));
  62.                     $possibleReleaseCustomer $this->customerRepository->search($criteria$event->getContext())->getElements();
  63.                     /** @var CustomerApprovalThresholdEntity|null $customerThreshold */
  64.                     $customerThreshold $event->getSalesChannelContext()->getCustomer()->getExtension('approvalThresholdExtension');
  65.                     $defaultReleaseCustomer $customerThreshold?->getDefaultReleaseCustomerId();
  66.                 }
  67.             }
  68.         }
  69.         $event->setParameter('freigeber'$freigeber);
  70.         $event->setParameter('cioPossibleReleaseCustomer'$possibleReleaseCustomer);
  71.         $event->setParameter('cioDefaultReleaseCustomer'$defaultReleaseCustomer);
  72.     }
  73. }