<?php
declare(strict_types=1);
namespace MadOrderNoPaymentChange\Subscriber;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedEvent;
use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class OrderSubscriber implements EventSubscriberInterface
{
public function __construct(
private SystemConfigService $systemConfigService,
private EntityRepository $orderRepository
) {
}
public static function getSubscribedEvents(): array
{
return [
AccountOverviewPageLoadedEvent::class => 'onAccountOverviewPageLoaded',
AccountOrderPageLoadedEvent::class => 'onAccountOrderPageLoaded',
AccountEditOrderPageLoadedEvent::class => 'onAccountOrderEditLoaded'
];
}
public function onAccountOrderEditLoaded(AccountEditOrderPageLoadedEvent $event): void
{
$paymentsNoChange = $this->systemConfigService->get('MadOrderNoPaymentChange.config.paymentsNotChange');
if (count($paymentsNoChange) > 0) {
$order = $event->getPage()->getOrder();
$orderTransactions = $order->getTransactions();
$lastTransaction = $orderTransactions->last();
if ($lastTransaction instanceof OrderTransactionEntity) {
$orderPaymentId = $lastTransaction->getPaymentMethodId();
if (in_array($orderPaymentId, $paymentsNoChange)) {
$noChange = new ArrayEntity(['noChangePayment' => true]);
$order->addExtension('noChangePayment', $noChange);
}
}
}
}
public function onAccountOverviewPageLoaded(AccountOverviewPageLoadedEvent $event): void
{
/* Abholen ob Zahlungsarten hinterlegt wurden */
$paymentsNoChange = $this->systemConfigService->get('MadOrderNoPaymentChange.config.paymentsNotChange');
if (count($paymentsNoChange) > 0) {
$newestOrder = $event->getPage()->getNewestOrder();
if ($newestOrder instanceof OrderEntity) {
$orderTransactions = $newestOrder->getTransactions();
$lastTransaction = $orderTransactions->last();
if ($lastTransaction instanceof OrderTransactionEntity) {
$orderPaymentId = $lastTransaction->getPaymentMethodId();
if (in_array($orderPaymentId, $paymentsNoChange)) {
$noChange = new ArrayEntity(['noChangePayment' => true]);
$newestOrder->addExtension('noChangePayment', $noChange);
}
}
}
}
/* EOF: Abholen ob Zahlungsarten hinterlegt wurden */
}
public function onAccountOrderPageLoaded(AccountOrderPageLoadedEvent $event): void
{
/* Abholen ob Zahlungsarten hinterlegt wurden */
$paymentsNoChange = $this->systemConfigService->get('MadOrderNoPaymentChange.config.paymentsNotChange');
if (count($paymentsNoChange) > 0) {
$orders = $event->getPage()->getOrders()->getElements();
/* @var $order OrderEntity */
foreach ($orders as $key => $order) {
$orderTransactions = $order->getTransactions();
$lastTransaction = $orderTransactions->last();
if ($lastTransaction instanceof OrderTransactionEntity) {
$orderPaymentId = $lastTransaction->getPaymentMethodId();
if (in_array($orderPaymentId, $paymentsNoChange)) {
$noChange = new ArrayEntity(['noChangePayment' => true]);
$order->addExtension('noChangePayment', $noChange);
}
}
}
}
/* EOF: Abholen ob Zahlungsarten hinterlegt wurden */
}
}