Skip to content

Commit

Permalink
Update Shipping Rates find logic (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
aashwin-rvvup authored Jan 8, 2025
1 parent c4066a1 commit c67bfa5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
4 changes: 2 additions & 2 deletions Service/Express/ExpressPaymentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public function updateShippingAddress(Quote $quote, array $address): array
->setPostcode($address['postcode'] ?? null)
->setCollectShippingRates(true);

$shippingMethods = $this->shippingMethodService->setFirstShippingMethodInQuote($quote, $shippingAddress)
["availableShippingMethods"];
$shippingMethods = $this->shippingMethodService->getAvailableShippingMethods($quote);
$this->shippingMethodService->setFirstShippingMethodInQuote($quote, $shippingAddress, $shippingMethods);

$quote->setTotalsCollectedFlag(false);
$quote->collectTotals();
Expand Down
53 changes: 30 additions & 23 deletions Service/Shipping/ShippingMethodService.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function updateShippingMethod(
?string $methodId
): Quote {
$shippingAddress = $quote->getShippingAddress();
$quote = $this->setShippingMethodInQuote($quote, $methodId, $shippingAddress)["quote"];
$quote = $this->setShippingMethodInQuote($quote, $methodId, $shippingAddress);

$quote->setTotalsCollectedFlag(false);
$quote->collectTotals();
Expand All @@ -70,43 +70,50 @@ public function updateShippingMethod(
/**
* @param Quote $quote
* @param Address $shippingAddress
* @return array returns a quote and availableShippingMethods
* @param ShippingMethod[]|null $availableShippingMethods
* @return Quote
*/
public function setFirstShippingMethodInQuote(
Quote $quote,
Quote\Address $shippingAddress
): array {
return $this->setShippingMethodInQuote($quote, null, $shippingAddress);
Quote\Address $shippingAddress,
?array $availableShippingMethods = null
): Quote {
return $this->setShippingMethodInQuote($quote, null, $shippingAddress, $availableShippingMethods);
}

/**
* @param Quote $quote
* @param string|null $methodId
* @param Address $shippingAddress
* @return array returns a quote and availableShippingMethods
* @param ShippingMethod[]|null $availableShippingMethods
* @return Quote
*/
public function setShippingMethodInQuote(
Quote $quote,
?string $methodId,
Quote\Address $shippingAddress
): array {
$availableMethods = $this->getAvailableShippingMethods($quote);
if (empty($availableMethods)) {
$shippingAddress->setShippingMethod('');
return ["quote" => $quote, "availableShippingMethods" => $availableMethods];
Quote\Address $shippingAddress,
?array $availableShippingMethods = null
): Quote {

if (empty($methodId)) {
if ($availableShippingMethods == null) {
$availableShippingMethods = $this->getAvailableShippingMethods($quote);
}
if (empty($availableShippingMethods)) {
$shippingAddress->setShippingMethod('');
return $quote;
}
$methodId = $availableShippingMethods[0]->getId();
}
if (empty($methodId)) {
$methodId = $availableMethods[0]->getId();
$shippingAddress->setShippingMethod('');
return $quote;
}

$isMethodAvailable = count(array_filter($availableMethods, function ($method) use ($methodId) {
return $method->getId() === $methodId;
})) > 0;
$carrierCodeToMethodCode = explode('_', $methodId);

if (!$isMethodAvailable || count($carrierCodeToMethodCode) !== 2) {
$rate = $shippingAddress->getShippingRateByCode($methodId);
if ($rate == null) {
$shippingAddress->setShippingMethod('');
return ["quote" => $quote, "availableShippingMethods" => $availableMethods];
return $quote;
}

$shippingAddress->setShippingMethod($methodId)->setCollectShippingRates(true)->collectShippingRates();
Expand All @@ -115,11 +122,11 @@ public function setShippingMethodInQuote(
$quote->getId(),
$this->shippingInformationFactory->create()
->setShippingAddress($shippingAddress)
->setShippingCarrierCode($carrierCodeToMethodCode[0])
->setShippingMethodCode($carrierCodeToMethodCode[1])
->setShippingCarrierCode($rate->getCarrier())
->setShippingMethodCode($rate->getMethod())
);

return ["quote" => $quote, "availableShippingMethods" => $availableMethods];
return $quote;
}

/**
Expand Down

0 comments on commit c67bfa5

Please sign in to comment.