Skip to content

Commit

Permalink
CHK-6101: Fix Incorrect Shipping Totals After Quote to Express Pay Or…
Browse files Browse the repository at this point in the history
…der Conversion (#103)

* Fix incorrect shipping totals after quote conversion (CHK-6101)

* Ensure address data is set before rate conversion (CHK-6101)

Fixes "INVALID_STRING_LENGTH" error for address country code caused by
sending shipping options without an address.

* Unset shipping method during Express Pay order creation (CHK-6101)

Fixes issue with shipping totals being calculated incorrectly.
  • Loading branch information
JosephLeedy authored Oct 25, 2024
1 parent 6b4f8e8 commit 16a4514
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
4 changes: 4 additions & 0 deletions Service/ExpressPay/Order/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public function execute($quoteMaskId, $gatewayId, $shippingStrategy): array
throw new LocalizedException(__('Could not create Express Pay order. Invalid quote ID "%1".', $quoteId));
}

if (!empty($quote->getShippingAddress()->getShippingMethod())) {
$quote->getShippingAddress()->setShippingMethod('');
}

$websiteId = (int)$quote->getStore()->getWebsiteId();
$uri = 'checkout/orders/{{shopId}}/wallet_pay';

Expand Down
57 changes: 34 additions & 23 deletions Service/ExpressPay/QuoteConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use function array_sum;
use function array_values;
use function ceil;
use function count;
use function in_array;
use function number_format;
use function str_replace;
Expand Down Expand Up @@ -124,11 +125,22 @@ public function convertShippingInformation(Quote $quote, bool $includeAddress =
return [];
}

$currencyCode = $quote->getCurrency() !== null ? $quote->getCurrency()->getQuoteCurrencyCode() : '';
if (!$this->areTotalsCollected) {
$shippingAddress->setCollectShippingRates(true);

$quote->collectTotals();

$shippingAddress->setCollectShippingRates(true);
$shippingAddress->collectShippingRates();
$this->areTotalsCollected = true;
}

$convertedQuote = [
'order_data' => [
'shipping_address' => [],
'selected_shipping_option' => [],
'shipping_options' => []
]
];
$currencyCode = $quote->getCurrency() !== null ? $quote->getCurrency()->getQuoteCurrencyCode() : '';
$usedRateCodes = [];
/** @var Rate[] $shippingRates */
$shippingRates = array_values(array_filter(
Expand All @@ -144,28 +156,25 @@ static function (Rate $rate) use (&$usedRateCodes): bool {
return true;
}
)); // Work-around for Magento bug causing duplicated shipping rates

$convertedQuote = [
'order_data' => [
'shipping_options' => array_map(
static function (Rate $rate) use ($currencyCode): array {
return [
'id' => $rate->getCode(),
'label' => trim("{$rate->getCarrierTitle()} - {$rate->getMethodTitle()}", ' -'),
'type' => 'SHIPPING',
'amount' => [
'currency_code' => $currencyCode ?? '',
'value' => number_format((float)$rate->getPrice(), 2)
]
];
},
$shippingRates
)
]
];

$hasRequiredAddressData = ($shippingAddress->getCity() && $shippingAddress->getCountryId());

if ($hasRequiredAddressData && count($shippingRates) > 0) {
$convertedQuote['order_data']['shipping_options'] = array_map(
static function (Rate $rate) use ($currencyCode): array {
return [
'id' => $rate->getCode(),
'label' => trim("{$rate->getCarrierTitle()} - {$rate->getMethodTitle()}", ' -'),
'type' => 'SHIPPING',
'amount' => [
'currency_code' => $currencyCode ?? '',
'value' => number_format((float)$rate->getPrice(), 2)
]
];
},
$shippingRates
);
}

if ($includeAddress && $hasRequiredAddressData) {
$convertedQuote['order_data']['shipping_address'] = [
'address_line_1' => $shippingAddress->getStreet()[0] ?? '',
Expand All @@ -189,6 +198,8 @@ static function (Rate $rate) use ($currencyCode): array {
];
}

$convertedQuote['order_data'] = array_filter($convertedQuote['order_data']);

return $convertedQuote;
}

Expand Down

0 comments on commit 16a4514

Please sign in to comment.