diff --git a/CHANGELOG.md b/CHANGELOG.md index be6806bc7d..ec3745dad7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/example-templates/dist/shop/checkout/_includes/order-summary.twig b/example-templates/dist/shop/checkout/_includes/order-summary.twig index 4084eea77b..5e37ad1090 100644 --- a/example-templates/dist/shop/checkout/_includes/order-summary.twig +++ b/example-templates/dist/shop/checkout/_includes/order-summary.twig @@ -71,7 +71,7 @@ {{- 'Shipping Method'|t -}}
- {% if not cart.shippingMethod %} + {% if not cart.shippingMethodName %} {{ 'None selected.'|t }} {{- 'Choose a shipping method.'|t -}} diff --git a/example-templates/src/shop/checkout/_includes/order-summary.twig b/example-templates/src/shop/checkout/_includes/order-summary.twig index 6d424923bc..6c1934f77e 100644 --- a/example-templates/src/shop/checkout/_includes/order-summary.twig +++ b/example-templates/src/shop/checkout/_includes/order-summary.twig @@ -71,7 +71,7 @@ {{- 'Shipping Method'|t -}}
- {% if not cart.shippingMethod %} + {% if not cart.shippingMethodName %} {{ 'None selected.'|t }} {{- 'Choose a shipping method.'|t -}} diff --git a/src/elements/Order.php b/src/elements/Order.php index 9355746282..6cebff8a53 100644 --- a/src/elements/Order.php +++ b/src/elements/Order.php @@ -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; @@ -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; } @@ -3034,6 +3050,7 @@ public function getShippingMethodId() /** * @return ShippingMethod|null + * @deprected in 3.4.18. Use `$shippingMethodHandle` or `$shippingMethodName` instead. */ public function getShippingMethod() {