custom/plugins/WabsIgmTheme/src/Subscriber/FrontendSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. namespace Wabs\IgmTheme\Subscriber;
  3. use PayonePayment\Components\TransactionStatus\TransactionStatusService;
  4. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionDefinition;
  6. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Shopware\Core\System\StateMachine\StateMachineRegistry;
  14. use Shopware\Core\System\StateMachine\Transition;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class FrontendSubscriber implements EventSubscriberInterface {
  17.     /**
  18.      * @var StateMachineRegistry
  19.      */
  20.     protected StateMachineRegistry $stateMachineRegistry;
  21.     /**
  22.      * @var EntityRepositoryInterface
  23.      */
  24.     protected EntityRepositoryInterface $paymentMethodRepository;
  25.     /**
  26.      * @param StateMachineRegistry $transactionStatusService
  27.      * @param EntityRepositoryInterface $paymentMethodRepository
  28.      */
  29.     public function __construct(StateMachineRegistry $transactionStatusServiceEntityRepositoryInterface $paymentMethodRepository) {
  30.         $this->stateMachineRegistry $transactionStatusService;
  31.         $this->paymentMethodRepository $paymentMethodRepository;
  32.     }
  33.     public static function getSubscribedEvents() {
  34.         return [
  35.             CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlaced'
  36.         ];
  37.     }
  38.     /**
  39.      * @param $methodName
  40.      * @param Context $context
  41.      * @return PaymentMethodEntity|null
  42.      */
  43.     private function getPaymentMethodByName($methodNameContext $context): ?PaymentMethodEntity {
  44.         $criteria = new Criteria();
  45.         $criteria->addFilter(new EqualsFilter('name'$methodName));
  46.         $criteria->addFilter(new EqualsFilter('active'true));
  47.         return $this->paymentMethodRepository->search($criteria$context)->first();
  48.     }
  49.     public function onCheckoutOrderPlaced(CheckoutOrderPlacedEvent $event) {
  50.         $order $event->getOrder();
  51.         $lastTransactions $order->getTransactions()->last();
  52.         $currentPaymentMethod $lastTransactions->getPaymentMethod();
  53.         $freePaymentMethod $this->getPaymentMethodByName('Keine (kostenfreie Bestellung)'$event->getContext());
  54.         if ($currentPaymentMethod && $freePaymentMethod) {
  55.             if ($currentPaymentMethod->getId() === $freePaymentMethod->getId()) {
  56.                 $this->stateMachineRegistry->transition(
  57.                     new Transition(
  58.                         OrderTransactionDefinition::ENTITY_NAME,
  59.                         $lastTransactions->getId(),
  60.                         'paid',
  61.                         'stateId'
  62.                     ),
  63.                     $event->getContext()
  64.                 );
  65.             }
  66.         }
  67.     }
  68. }