Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling getShippingMethod() on a cart returns nothing for Postie-provided shipping methods #2279

Closed
engram-design opened this issue Jul 21, 2021 · 3 comments

Comments

@engram-design
Copy link
Contributor

In Commerce 3.4.x, calling {{ cart.shippingMethod.name }} now returns null during checkout, despite having the shipping method applied on the order, which can be seen reflected in the control panel for the active order. This is happening for Postie-provided shipping methods, which are provided on-the-fly.

This is due to refactored code for Order::getShippingMethod() which calls getShippingMethodByHandle() which in turn calls getAllShippingMethods().

My first issue is that the docblock for the getAllShippingMethods() method states:

Returns the Commerce managed and 3rd party shipping methods

Which doesn't seem true at all, and even in previous Commerce versions, this has never been the case. Here, it should return both "normal" shipping methods defined by the user in the control panel for Commerce's setting, and any shipping methods provided by plugins or modules. It has only ever done the former?

Secondly, the Order::getShippingMethod() has drastically changed from 3.3.5.1

public function getShippingMethod()
{
$shippingMethods = Plugin::getInstance()->getShippingMethods()->getAvailableShippingMethods($this);
// Do we have a shipping method available based on the current selection?
if ($shippingMethod = ArrayHelper::firstWhere($shippingMethods, 'handle', $this->shippingMethodHandle)) {
return $shippingMethod;
}
$handles = ArrayHelper::getColumn($shippingMethods, 'handle');
if (!empty($handles)) {
/** @var ShippingMethod $firstAvailable */
$firstAvailable = ArrayHelper::firstValue($shippingMethods);
if ($this->shippingMethodHandle && !in_array($this->shippingMethodHandle, $handles, false)) {
$message = Craft::t('commerce', 'Previously selected shipping method is no longer available.');
$this->addNotice(
Craft::createObject([
'class' => OrderNotice::class,
'attributes' => [
'type' => 'shippingMethodChanged',
'attribute' => 'shippingMethodHandle',
'message' => $message,
]
])
);
$this->shippingMethodHandle = null;
}
if (!$this->shippingMethodHandle || !in_array($this->shippingMethodHandle, $handles, false)) {
$this->shippingMethodHandle = $firstAvailable->getHandle();
}
}
return ArrayHelper::firstWhere($shippingMethods, 'handle', $this->shippingMethodHandle);
}

Most importantly, the previous function was using Plugin::getInstance()->getShippingMethods()->getAvailableShippingMethods() which was returning the correct collection because it actually includes the calls to EVENT_REGISTER_AVAILABLE_SHIPPING_METHODS which plugins and modules use.

So I suppose one of these sides needs fixing. Either the Order getShippingMethod() should call getAvailableShippingMethods() or the getAllShippingMethods() service method needs to include shipping methods provided by the event. Or - something else I'm missing.

Also refer to issue verbb/postie#72 which is instance of the issue.

Additional info

  • Craft version: 3.7.4
  • Commerce version: 3.4.0.2
  • PHP version: 7.2
  • Other Plugins & versions: Postie 2.4.8
@pierrestoffe
Copy link

I'm encountering the same issue with shipping methods that are defined in a plugin via the EVENT_REGISTER_AVAILABLE_SHIPPING_METHODS event.

@lukeholder
Copy link
Member

This should now be fixed in 3.4.0.3

I installed Postie, configured Fastway, and can confirm that I can select a Fastway shipping option and have the price applied to the order:

CleanShot.2021-07-22.at.13.28.17.mp4

@engram-design
Copy link
Contributor Author

@lukeholder Can confirm this works, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants