custom/plugins/AcrisCheckoutMethodPreselectCS/src/Core/Checkout/Cart/CartRuleLoader.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CheckoutMethodPreselect\Core\Checkout\Cart;
  3. use Acris\CheckoutMethodPreselect\Components\ValidatorCallbackService;
  4. use Shopware\Core\Checkout\Cart\Cart;
  5. use Shopware\Core\Checkout\Cart\CartBehavior;
  6. use Shopware\Core\Checkout\Cart\Error\Error;
  7. use Shopware\Core\Checkout\Cart\RuleLoaderResult;
  8. use Shopware\Core\Checkout\Payment\Cart\Error\PaymentMethodBlockedError;
  9. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  10. use Shopware\Core\Checkout\Shipping\Cart\Error\ShippingMethodBlockedError;
  11. use Shopware\Core\Checkout\Shipping\ShippingMethodEntity;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. class CartRuleLoader extends \Shopware\Core\Checkout\Cart\CartRuleLoader
  14. {
  15.     /**
  16.      * @var \Shopware\Core\Checkout\Cart\CartRuleLoader
  17.      */
  18.     private $parent;
  19.     /**
  20.      * @var ValidatorCallbackService
  21.      */
  22.     private $validatorCallbackService;
  23.     public function __construct(
  24.         ValidatorCallbackService $validatorCallbackService,
  25.         \Shopware\Core\Checkout\Cart\CartRuleLoader $parent
  26.     ) {
  27.         $this->parent $parent;
  28.         $this->validatorCallbackService $validatorCallbackService;
  29.     }
  30.     public function loadByToken(SalesChannelContext $contextstring $cartToken): RuleLoaderResult
  31.     {
  32.         $ruleLoaderResult $this->parent->loadByToken($context$cartToken);
  33.         return $this->checkMethodPreselection($ruleLoaderResult$context);
  34.     }
  35.     public function loadByCart(SalesChannelContext $contextCart $cartCartBehavior $behaviorContextbool $isNew false): RuleLoaderResult
  36.     {
  37.         $ruleLoaderResult $this->parent->loadByCart($context$cart$behaviorContext$isNew);
  38.         return $this->checkMethodPreselection($ruleLoaderResult$context);
  39.     }
  40.     public function reset(): void
  41.     {
  42.         $this->parent->reset();
  43.     }
  44.     public function invalidate(): void
  45.     {
  46.         $this->parent->invalidate();
  47.     }
  48.     private function checkMethodPreselection(RuleLoaderResult $ruleLoaderResultSalesChannelContext $context): RuleLoaderResult
  49.     {
  50.         if($ruleLoaderResult->getCart()->getErrors() && $ruleLoaderResult->getCart()->getErrors()->count() > 0) {
  51.             $paymentMethod null;
  52.             $shippingMethod null;
  53.             /** @var Error $cartError */
  54.             foreach ($ruleLoaderResult->getCart()->getErrors()->getElements() as $key => $cartError) {
  55.                 if($cartError instanceof PaymentMethodBlockedError) {
  56.                     $paymentMethod $this->validatorCallbackService->getFirstActivePaymentMethod($context);
  57.                     if($paymentMethod instanceof PaymentMethodEntity) {
  58.                         $ruleLoaderResult->getCart()->getErrors()->remove($key);
  59.                         if($context->getPaymentMethod() && $paymentMethod->getId() === $context->getPaymentMethod()->getId()) {
  60.                             $paymentMethod null;
  61.                         }
  62.                     }
  63.                 }
  64.                 if($cartError instanceof ShippingMethodBlockedError) {
  65.                     $shippingMethod $this->validatorCallbackService->getFirstActiveShippingMethod($context);
  66.                     if($shippingMethod instanceof ShippingMethodEntity) {
  67.                         $ruleLoaderResult->getCart()->getErrors()->remove($key);
  68.                         if($context->getShippingMethod() && $shippingMethod->getId() === $context->getShippingMethod()->getId()) {
  69.                             $shippingMethod null;
  70.                         }
  71.                     }
  72.                 }
  73.             }
  74.             if($paymentMethod instanceof PaymentMethodEntity || $shippingMethod instanceof ShippingMethodEntity) {
  75.                 return $this->changeMethods($paymentMethod$shippingMethod$ruleLoaderResult->getCart(), $context);
  76.             }
  77.         }
  78.         return $ruleLoaderResult;
  79.     }
  80.     private function changeMethods(?PaymentMethodEntity $paymentMethod, ?ShippingMethodEntity $shippingMethodCart $cartSalesChannelContext $context): RuleLoaderResult
  81.     {
  82.         if($paymentMethod instanceof PaymentMethodEntity) {
  83.             $context->assign(['paymentMethod' => $paymentMethod]);
  84.         }
  85.         if($shippingMethod instanceof ShippingMethodEntity) {
  86.             $context->assign(['shippingMethod' => $shippingMethod]);
  87.         }
  88.         return $this->parent->loadByCart($context$cart, new CartBehavior($context->getPermissions()));
  89.     }
  90. }