custom/plugins/MadOrderNoPaymentChange/src/Subscriber/OrderSubscriber.php line 56

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MadOrderNoPaymentChange\Subscriber;
  4. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
  5. use Shopware\Core\Checkout\Order\OrderEntity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  8. use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedEvent;
  9. use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Shopware\Core\Framework\Struct\ArrayEntity;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. class OrderSubscriber implements EventSubscriberInterface
  14. {
  15.     public function __construct(
  16.         private SystemConfigService $systemConfigService,
  17.         private EntityRepository $orderRepository
  18.     ) {
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             AccountOverviewPageLoadedEvent::class => 'onAccountOverviewPageLoaded',
  24.             AccountOrderPageLoadedEvent::class => 'onAccountOrderPageLoaded',
  25.             AccountEditOrderPageLoadedEvent::class => 'onAccountOrderEditLoaded'
  26.         ];
  27.     }
  28.     public function onAccountOrderEditLoaded(AccountEditOrderPageLoadedEvent $event): void
  29.     {
  30.         $paymentsNoChange $this->systemConfigService->get('MadOrderNoPaymentChange.config.paymentsNotChange');
  31.         if (count($paymentsNoChange) > 0) {
  32.             $order $event->getPage()->getOrder();
  33.             $orderTransactions $order->getTransactions();
  34.             $lastTransaction $orderTransactions->last();
  35.             if ($lastTransaction instanceof OrderTransactionEntity) {
  36.                 $orderPaymentId $lastTransaction->getPaymentMethodId();
  37.                 if (in_array($orderPaymentId$paymentsNoChange)) {
  38.                     $noChange = new ArrayEntity(['noChangePayment' => true]);
  39.                     $order->addExtension('noChangePayment'$noChange);
  40.                 }
  41.             }
  42.         }
  43.     }
  44.     public function onAccountOverviewPageLoaded(AccountOverviewPageLoadedEvent $event): void
  45.     {
  46.         /* Abholen ob Zahlungsarten hinterlegt wurden */
  47.         $paymentsNoChange $this->systemConfigService->get('MadOrderNoPaymentChange.config.paymentsNotChange');
  48.         if (count($paymentsNoChange) > 0) {
  49.             $newestOrder $event->getPage()->getNewestOrder();
  50.             if ($newestOrder instanceof OrderEntity) {
  51.                 $orderTransactions $newestOrder->getTransactions();
  52.                 $lastTransaction $orderTransactions->last();
  53.                 if ($lastTransaction instanceof OrderTransactionEntity) {
  54.                     $orderPaymentId $lastTransaction->getPaymentMethodId();
  55.                     if (in_array($orderPaymentId$paymentsNoChange)) {
  56.                         $noChange = new ArrayEntity(['noChangePayment' => true]);
  57.                         $newestOrder->addExtension('noChangePayment'$noChange);
  58.                     }
  59.                 }
  60.             }
  61.         }
  62.         /* EOF: Abholen ob Zahlungsarten hinterlegt wurden */
  63.     }
  64.     public function onAccountOrderPageLoaded(AccountOrderPageLoadedEvent $event): void
  65.     {
  66.         /* Abholen ob Zahlungsarten hinterlegt wurden */
  67.         $paymentsNoChange $this->systemConfigService->get('MadOrderNoPaymentChange.config.paymentsNotChange');
  68.         if (count($paymentsNoChange) > 0) {
  69.             $orders $event->getPage()->getOrders()->getElements();
  70.             /* @var $order OrderEntity */
  71.             foreach ($orders as $key => $order) {
  72.                 $orderTransactions $order->getTransactions();
  73.                 $lastTransaction $orderTransactions->last();
  74.                 if ($lastTransaction instanceof OrderTransactionEntity) {
  75.                     $orderPaymentId $lastTransaction->getPaymentMethodId();
  76.                     if (in_array($orderPaymentId$paymentsNoChange)) {
  77.                         $noChange = new ArrayEntity(['noChangePayment' => true]);
  78.                         $order->addExtension('noChangePayment'$noChange);
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.         /* EOF: Abholen ob Zahlungsarten hinterlegt wurden */
  84.     }
  85. }