custom/plugins/EnetMinOrderValue/src/Subscriber/ValidationSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. namespace EnetMinOrderValue\Subscriber;
  3. use EnetMinOrderValue\Validator\MinOrderValue;
  4. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  5. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  6. use Shopware\Core\Framework\Validation\DataValidationDefinition;
  7. use Shopware\Core\Framework\Validation\DataValidator;
  8. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. class ValidationSubscriber implements EventSubscriberInterface
  14. {
  15.     /** @var DataValidator $validator */
  16.     protected $validator;
  17.     /** @var RequestStack $requestStack */
  18.     protected $requestStack;
  19.     /** @var SystemConfigService $systemConfigService */
  20.     protected $systemConfigService;
  21.     /** @var CartService $cartService */
  22.     protected $cartService;
  23.     public function __construct(DataValidator $validatorRequestStack $requestStackCartService $cartServiceSystemConfigService $systemConfigService)
  24.     {
  25.         $this->validator $validator;
  26.         $this->requestStack $requestStack;
  27.         $this->cartService $cartService;
  28.         $this->systemConfigService $systemConfigService;
  29.     }
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             'framework.validation.order.create' => 'cartValidation'
  34.         ];
  35.     }
  36.     public function cartValidation(BuildValidationEvent $event)
  37.     {
  38.         $minOrderValue floatval($this->systemConfigService->get('EnetMinOrderValue.config.minOrderValue'));
  39.         /** @var \Shopware\Core\System\SalesChannel\SalesChannelContext $salesChannelContext */
  40.         $salesChannelContext $this->requestStack->getCurrentRequest()->get('sw-sales-channel-context');
  41.         // only validate if orderValue is larger then 0 (default for off)
  42.         // or if paymentMethod is not UserCreditPayment
  43. //        dump($salesChannelContext->getPaymentMethod()->getFormattedHandlerIdentifier());
  44. //        die();
  45.         if($minOrderValue && $salesChannelContext->getPaymentMethod()->getFormattedHandlerIdentifier() !== "handler_enetcredit_usercreditpayment") {
  46.             $data $this->requestStack->getCurrentRequest()->request;
  47.             $definition = new DataValidationDefinition('order.minOrderValue');
  48.             $cart $this->cartService->getCart($salesChannelContext->getToken(), $salesChannelContext);
  49.             $definition->add('enet_minOrderValue',
  50.                 new MinOrderValue($minOrderValue$cart->getPrice())
  51.             );
  52.             $violations $this->validator->getViolations($data->all(), $definition);
  53.             if(!$violations->count()) {
  54.                 return;
  55.             }
  56.             throw new ConstraintViolationException($violations$data->all());
  57.         }
  58.     }
  59. }