custom/plugins/EnetCredit/src/Subscriber/PaymentMethodSubscriber.php line 54

Open in your IDE?
  1. <?php
  2. namespace EnetCredit\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Cart;
  4. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  5. use Shopware\Core\Checkout\Payment\PaymentEvents;
  6. use Shopware\Core\Defaults;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  10. use Shopware\Core\Framework\Uuid\Uuid;
  11. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory;
  12. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. class PaymentMethodSubscriber implements EventSubscriberInterface
  18. {
  19.     /** @var RequestStack $requstStack */
  20.     protected $requstStack;
  21.     /** @var CartService $cartService */
  22.     protected $cartService;
  23.     /** @var SalesChannelContextFactory $salesChanelContextFactory */
  24.     protected $salesChanelContextFactory;
  25.     /** @var SystemConfigService $systemConfigService */
  26.     protected $systemConfigService;
  27.     public function __construct(
  28.         SalesChannelContextFactory $salesChannelContextFactory,
  29.         CartService $cartService,
  30.         RequestStack $requestStack,
  31.         SystemConfigService $systemConfigService
  32.     )
  33.     {
  34.         $this->requstStack $requestStack;
  35.         $this->cartService $cartService;
  36.         $this->salesChanelContextFactory $salesChannelContextFactory;
  37.         $this->systemConfigService $systemConfigService;
  38.     }
  39.     public static function getSubscribedEvents()
  40.     {
  41.         return [
  42.             PaymentEvents::PAYMENT_METHOD_SEARCH_RESULT_LOADED_EVENT => 'paymentLoaded'
  43.         ];
  44.     }
  45.     public function paymentLoaded(EntitySearchResultLoadedEvent $event)
  46.     {
  47.         $systemConfigEnabled $this->systemConfigService->get('EnetCredit.config.notBuyableWithUserCredit');
  48.         if(!$systemConfigEnabled || $systemConfigEnabled === false) {
  49.             return;
  50.         }
  51.         $cart $this->getCustomerCart($event);
  52.         $userCreditId null;
  53.         foreach($event->getResult()->getEntities() as $entity) {
  54.             if($entity->getFormattedHandlerIdentifier() === 'handler_enetcredit_usercreditpayment') {
  55. //                dump($entity, $entity->getFormattedHandlerIdentifier());
  56.                 $userCreditId $entity->getId();
  57. //                dump('userCreditSet', $userCreditId);
  58.             }
  59.         }
  60. //
  61. //        if(!$userCreditId) {
  62. //            return;
  63. //        }
  64.         foreach($cart->getLineItems()->getElements() as $lineItem) {
  65. //            dump($lineItem);
  66.             $customField $lineItem->getPayloadValue('customFields');
  67. //            dump($customField);
  68.             if(isset($customField['enet_credit_product']) && $customField['enet_credit_product'] === true) {
  69. //                dump('product is special');
  70.                 $event->getResult()->getEntities()->remove($userCreditId);
  71. //                dump('removed');
  72. //                die();
  73.             }
  74.         }
  75. //        die();
  76. //        $event->getResult()->getEntities()->remove('662dd863223d43f49f623c30784f9b45');
  77. //
  78. //        dump($event, $event->getResult()->getEntities()->getElements());
  79. //        die();
  80. //
  81. //
  82. //        foreach($event->getEntities() as $entity) {
  83. //
  84. //            if($entity->getFormattedHandlerIdentifier() === 'handler_enetcredit_usercreditpayment') {
  85. //                dump($entity, $entity->getFormattedHandlerIdentifier());
  86. //
  87. //
  88. //                die();
  89. //            }
  90. //        }
  91.     }
  92.     private function getCustomerCart(EntitySearchResultLoadedEvent $event): Cart
  93.     {
  94.         $session $this->requstStack->getMasterRequest()->getSession();
  95.         $token $session->getBag('attributes')->get('sw-context-token');
  96.         $salesChannelContext $this->salesChanelContextFactory->create($token$event->getContext()->getSource()->getSalesChannelId());
  97.         $cart $this->cartService->getCart($token$salesChannelContext);
  98.         return $cart;
  99.     }
  100. }