craftcms / commerce

Fully integrated ecommerce for Craft CMS.
https://craftcms.com/commerce
Other
219 stars 170 forks source link

[4.x]: shippingMethodName erased after updating order in CP #3519

Closed jmauzyk closed 4 months ago

jmauzyk commented 4 months ago

What happened?

Description

For completed orders that have selected a shipping method provided by a plugin, the shippingMethodName is erased from the order record after editing and updating in the control panel. This is because on line 1335 of craft\commerce\controllers\OrdersController it uses the following code to update the shippingMethodName on the order:

$shippingMethod = $order->shippingMethodHandle ? Plugin::getInstance()->getShippingMethods()->getShippingMethodByHandle($order->shippingMethodHandle) : null;
$order->shippingMethodName = $shippingMethod->name ?? null;

Because the shipping methods service only queries those provided by Commerce's shipping engine, it does not find others provided by plugins, and sets the shippingMethodName to null.

This could be avoided by instead using a method like in line 1882 of craft\commerce\elements\Order:

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

Steps to reproduce

  1. Create and complete an order from the front end, selecting a shipping method provided by a plugin.
  2. Observe that shippingMethodName is correctly saved on the order record.
  3. Open the order's edit page in the CP and edit, then click Update.
  4. Observe that shippingMethodName is now empty on the order record.

Expected behavior

Maintain shippingMethodName for plugin shipping methods after order update.

Actual behavior

shippingMethodName is erased after update for plugin shipping methods.

Craft CMS version

4.9.5 Pro

Craft Commerce version

4.6.1

PHP version

8.2.19

Operating system and version

Linux 6.6.26-linuxkit

Database type and version

PostgreSQL 14.9

Image driver and version

Imagick 3.7.0 (ImageMagick 6.9.11-60)

Installed plugins and versions

No response

linear[bot] commented 4 months ago

PT-1758 [4.x]: shippingMethodName erased after updating order in CP

lukeholder commented 4 months ago

@jmauzyk Thanks this has been fixed for the next Commerce 4.x release.

To get the fix early, change your craftcms/commerce requirement in composer.json to:

"require": {
  "craftcms/commerce": "4.x-dev#0d3e98464e66cafe43eda1a22ac4ab4e53008f0a as 4.6.1",
  "...": "..."
}

Then run composer update.

We will update this ticket once the release is out. Thanks.

brandonkelly commented 4 months ago

Commerce 4.6.2 and 5.0.8 are out with that fix. Thanks again!