Skip to content

Commit

Permalink
#2986 fixing the usage of custom shipping methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Oct 7, 2022
1 parent 8fa5c6e commit 6bf9ebd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

## Unreleased

### Deprecated
- Deprecated `craft\commerce\elements\Order::getShippingMethod()`. Use `$shippingMethodName` and `$shippingMethodHandle` instead.

### Fixed
- Fixed a bug that occurred when using a custom shipping method. ([#2986](https://github.com/craftcms/commerce/issues/2986))
- Fixed a bug that could occur when querying products by `type`. ([#2966](https://github.com/craftcms/commerce/issues/2966))

## 3.4.17.2 - 2022-09-16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
{{- 'Shipping Method'|t -}}
</h4>
<div>
{% if not cart.shippingMethod %}
{% if not cart.shippingMethodName %}
{{ 'None selected.'|t }}
<a href="{{ url('/shop/checkout/shipping') }}" class="text-blue-500 hover:text-blue-600">
{{- 'Choose a shipping method.'|t -}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
{{- 'Shipping Method'|t -}}
</h4>
<div>
{% if not cart.shippingMethod %}
{% if not cart.shippingMethodName %}
{{ 'None selected.'|t }}
<a href="{{ url('/[[folderName]]/checkout/shipping') }}" class="[[classes.a]]">
{{- 'Choose a shipping method.'|t -}}
Expand Down
25 changes: 21 additions & 4 deletions src/elements/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -1766,8 +1766,11 @@ public function recalculate()

if (!$this->shippingMethodHandle) {
$this->shippingMethodName = null;
} elseif ($shippingMethod = $this->getShippingMethod()) {
$this->shippingMethodName = $shippingMethod->getName();
} else {
$shippingMethod = ArrayHelper::firstWhere($this->getAvailableShippingMethodOptions(), 'handle', $this->shippingMethodHandle);
if ($shippingMethod) {
$this->shippingMethodName = $shippingMethod->getName();
}
}

$lineItemRemoved = false;
Expand Down Expand Up @@ -1878,10 +1881,23 @@ public function getAvailableShippingMethodOptions(): array
$attributes = (new ShippingMethod())->attributes();

foreach ($availableMethods as $method) {
$option = new ShippingMethodOption($method->getAttributes($attributes));
$option = new ShippingMethodOption();

if ($method instanceof ShippingMethod) {
// TODO remove at a breaking change version
foreach (['dateCreated', 'dateUpdated'] as $attribute) {
$option->$attribute = $method->$attribute;
}
}

$option->setOrder($this);
$option->matchesOrder = true;
$option->enabled = $method->getIsEnabled();
$option->id = $method->getId();
$option->name = $method->getName();
$option->handle = $method->getHandle();
$option->matchesOrder = ArrayHelper::isIn($method->handle, $availableMethods);
$option->price = $method->getPriceForOrder($this);

$options[$option->getHandle()] = $option;
}

Expand Down Expand Up @@ -3034,6 +3050,7 @@ public function getShippingMethodId()

/**
* @return ShippingMethod|null
* @deprected in 3.4.18. Use `$shippingMethodHandle` or `$shippingMethodName` instead.
*/
public function getShippingMethod()
{
Expand Down

0 comments on commit 6bf9ebd

Please sign in to comment.