<?php
namespace EnetCredit\Subscriber;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Checkout\Payment\PaymentEvents;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class PaymentMethodSubscriber implements EventSubscriberInterface
{
/** @var RequestStack $requstStack */
protected $requstStack;
/** @var CartService $cartService */
protected $cartService;
/** @var SalesChannelContextFactory $salesChanelContextFactory */
protected $salesChanelContextFactory;
/** @var SystemConfigService $systemConfigService */
protected $systemConfigService;
public function __construct(
SalesChannelContextFactory $salesChannelContextFactory,
CartService $cartService,
RequestStack $requestStack,
SystemConfigService $systemConfigService
)
{
$this->requstStack = $requestStack;
$this->cartService = $cartService;
$this->salesChanelContextFactory = $salesChannelContextFactory;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents()
{
return [
PaymentEvents::PAYMENT_METHOD_SEARCH_RESULT_LOADED_EVENT => 'paymentLoaded'
];
}
public function paymentLoaded(EntitySearchResultLoadedEvent $event)
{
$systemConfigEnabled = $this->systemConfigService->get('EnetCredit.config.notBuyableWithUserCredit');
if(!$systemConfigEnabled || $systemConfigEnabled === false) {
return;
}
$cart = $this->getCustomerCart($event);
$userCreditId = null;
foreach($event->getResult()->getEntities() as $entity) {
if($entity->getFormattedHandlerIdentifier() === 'handler_enetcredit_usercreditpayment') {
// dump($entity, $entity->getFormattedHandlerIdentifier());
$userCreditId = $entity->getId();
// dump('userCreditSet', $userCreditId);
}
}
//
// if(!$userCreditId) {
// return;
// }
foreach($cart->getLineItems()->getElements() as $lineItem) {
// dump($lineItem);
$customField = $lineItem->getPayloadValue('customFields');
// dump($customField);
if(isset($customField['enet_credit_product']) && $customField['enet_credit_product'] === true) {
// dump('product is special');
$event->getResult()->getEntities()->remove($userCreditId);
// dump('removed');
// die();
}
}
// die();
// $event->getResult()->getEntities()->remove('662dd863223d43f49f623c30784f9b45');
//
// dump($event, $event->getResult()->getEntities()->getElements());
// die();
//
//
// foreach($event->getEntities() as $entity) {
//
// if($entity->getFormattedHandlerIdentifier() === 'handler_enetcredit_usercreditpayment') {
// dump($entity, $entity->getFormattedHandlerIdentifier());
//
//
// die();
// }
// }
}
private function getCustomerCart(EntitySearchResultLoadedEvent $event): Cart
{
$session = $this->requstStack->getMasterRequest()->getSession();
$token = $session->getBag('attributes')->get('sw-context-token');
$salesChannelContext = $this->salesChanelContextFactory->create($token, $event->getContext()->getSource()->getSalesChannelId());
$cart = $this->cartService->getCart($token, $salesChannelContext);
return $cart;
}
}