vendor/store.shopware.com/acrischeckoutmethodpreselectcs/src/Core/Checkout/Cart/CartRuleLoader.php line 32

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