diff --git a/src/Services/CheckoutService.php b/src/Services/CheckoutService.php index 4fe2d338e..ffe2b1106 100644 --- a/src/Services/CheckoutService.php +++ b/src/Services/CheckoutService.php @@ -6,6 +6,7 @@ use IO\Events\Checkout\CheckoutReadonlyChanged; use IO\Helper\ArrayHelper; use IO\Helper\MemoryCache; +use IO\Helper\Utils; use Plenty\Modules\Accounting\Contracts\AccountingLocationRepositoryContract; use Plenty\Modules\Basket\Contracts\BasketRepositoryContract; use Plenty\Modules\Basket\Events\Basket\AfterBasketChanged; @@ -29,7 +30,6 @@ use Plenty\Plugin\Events\Dispatcher; use Plenty\Plugin\Log\Loggable; use Plenty\Plugin\Translation\Translator; -use IO\Helper\Utils; /** * Class CheckoutService @@ -409,7 +409,7 @@ public function getMethodOfPaymentId() * @var PaymentMethod $methodOfPayment */ foreach ($methodOfPaymentList as $methodOfPaymentKey => $methodOfPayment) { - if($basket->id > 0 && $basketAmount <= 0 && $methodOfPayment->paymentKey !== self::ALREADY_PAID_PAYMENT_KEY) { + if ($basket->id > 0 && $basketAmount <= 0 && $methodOfPayment->paymentKey !== self::ALREADY_PAID_PAYMENT_KEY) { unset($methodOfPaymentList[$methodOfPaymentKey]); continue; } @@ -421,7 +421,7 @@ public function getMethodOfPaymentId() if ($methodOfPaymentID === null || !$methodOfPaymentValid) { $methodOfPayment = array_shift($methodOfPaymentList); - if(!is_null($methodOfPayment)) { + if (!is_null($methodOfPayment)) { $methodOfPaymentID = $methodOfPayment->id; } @@ -461,9 +461,11 @@ public function preparePayment(): array $basketService = pluginApp(BasketService::class); $validateCheckoutEvent = $this->checkout->validateCheckout(); - if ($validateCheckoutEvent instanceof ValidateCheckoutEvent && !empty( + if ( + $validateCheckoutEvent instanceof ValidateCheckoutEvent && !empty( $validateCheckoutEvent->getErrorKeysList() - )) { + ) + ) { $dispatcher = pluginApp(Dispatcher::class); if ($dispatcher instanceof Dispatcher) { $dispatcher->fire(pluginApp(AfterBasketChanged::class), []); @@ -533,12 +535,12 @@ function () { $methodOfPaymentList = []; $basket = $this->basketRepository->load(); if ($basket->basketAmount <= 0 && $basket->id > 0) { - $methodOfPaymentList = array_filter($methodOfPaymentListOriginal, function($methodOfPayment) { + $methodOfPaymentList = array_filter($methodOfPaymentListOriginal, function ($methodOfPayment) { return $methodOfPayment->paymentKey === self::ALREADY_PAID_PAYMENT_KEY; }); } - if(!count($methodOfPaymentList)) { + if (!count($methodOfPaymentList)) { $methodOfPaymentList = $methodOfPaymentListOriginal; } @@ -665,6 +667,24 @@ function () { if (!is_null($order)) { $isNet = $order->isNet; } + $basket = $this->basketService->getBasket(); + $fallbackShippingProfileId = null; + $exists = false; + if (is_array($list)) { + foreach ($list as $key => $shippingProfile) { + if (is_null($fallbackShippingProfileId)) { + $fallbackShippingProfileId = $shippingProfile['parcelServicePresetId']; + } + if ($basket->shippingProfileId == $shippingProfile['parcelServicePresetId']) { + $exists = true; + break; + } + } + } + if ($exists === false && !is_null($fallbackShippingProfileId)) { + $this->setShippingProfileId($fallbackShippingProfileId, true); + } + if (($isNet && !(bool)$accountSettings->showShippingVat) || (!$isNet && $showNetPrice)) { $maxVatValue = $this->basketService->getMaxVatValue(); @@ -677,7 +697,6 @@ function () { } } - $basket = $this->basketService->getBasket(); if ($basket->currency !== $this->currencyExchangeRepo->getDefaultCurrency()) { if (is_array($list)) { foreach ($list as $key => $shippingProfile) { @@ -732,10 +751,11 @@ public function getShippingProfileId(): int * Set the ID of the current shipping profile. * * @param int $shippingProfileId Id of the shipping profile to select. + * @param bool $force Force to set the shipping profile id. */ - public function setShippingProfileId(int $shippingProfileId) + public function setShippingProfileId(int $shippingProfileId, bool $force = false) { - $this->checkout->setShippingProfileId($shippingProfileId); + $this->checkout->setShippingProfileId($shippingProfileId, $force); } /** diff --git a/tests/Unit/Services/CheckoutServiceShippingTest.php b/tests/Unit/Services/CheckoutServiceShippingTest.php index 57c2767c8..83ec4e094 100644 --- a/tests/Unit/Services/CheckoutServiceShippingTest.php +++ b/tests/Unit/Services/CheckoutServiceShippingTest.php @@ -205,6 +205,18 @@ public function it_gets_the_right_shipping_profiles($shippingList) ->andReturn([]); $this->checkoutMock->shouldReceive('getShippingCountryId')->andReturn(10); + $this->checkoutMock->shouldReceive('setShippingProfileId')->with( + $shippingList[0]['parcelServicePresetId'], + true + ); + + $checkoutServiceMock = Mockery::mock(CheckoutService::class); + $this->replaceInstanceByMock(CheckoutService::class, $checkoutServiceMock); + + $checkoutServiceMock->shouldReceive('setShippingProfileId')->with( + $shippingList[0]['parcelServicePresetId'], + true + ); $this->applicationMock->shouldReceive('getWebstoreId')->andReturn(1);