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

[4.x]: Shipping Method Options no longer allow extra properties/data #3271

Closed
engram-design opened this issue Sep 8, 2023 · 2 comments
Closed
Labels
bug commerce4 Issues related to Commerce v4

Comments

@engram-design
Copy link
Contributor

What happened?

Referencing this issue and the Postie plugin.

Postie registers shipping methods, that are attached with ShippingMethods::EVENT_REGISTER_AVAILABLE_SHIPPING_METHODS. We use our own ShippingMethod class that extends the Commerce ShippingMethod class. In this, we store information about the carrier used (like UPS), and for each rate and service returned from the API, we store additional information like estimated delivery - not just the price.

What Commerce does is grab all available shipping methods, and create ShippingMethodOption models. I'm not sure why this is done, but it's been this way since Commerce 3.x I believe. Users then use cart.availableShippingMethodOptions on their shipping page.

The issue we have is that through this translation of ShippingMethod to ShippingMethodOption, we lose Postie's extra information. Despite ShippingMethodOption also extending from a ShippingMethod class, it's the Commerce class that's extended, and not Postie's. As such, any reference to the extra things Postie does is lost.

Might I suggest recording the original ShippingMethod that was used to create the ShippingMethodOption be added? We could add the following here:

$option->shippingMethod = $method;

Which would store Postie's shipping method against the ShippingMethodOption for people to access in their templates.

Other options are:

  • Adding in an extra property like data for storing arbitrary data for a ShippingMethodOption
  • Adding an event for creating a snapshot - similar to Products/Variants and line items

Craft CMS version

4.5.3

Craft Commerce version

4.2.11

PHP version

No response

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

@engram-design engram-design added commerce4 Issues related to Commerce v4 bug labels Sep 8, 2023
johnnynotsolucky added a commit to johnnynotsolucky/commerce that referenced this issue Sep 12, 2023
a ShippingMethodOption

Resolves craftcms#3271

Craft Commerce's ShippingMethodOption erases the information of any
custom ShippingMethod's that were used to create them. While not ideal,
this change sets a new field, shippingMethod on the ShippingMethodOption
class and also proxies the call to getShippingRules to use the attached
ShippingMethod if it is present.

Of course, other functions such as getName, getType, etc will not be
overridden and would need to be accessed like
$option->shippingMethod->getType().
johnnynotsolucky added a commit to johnnynotsolucky/commerce that referenced this issue Sep 12, 2023
a ShippingMethodOption

Resolves craftcms#3271

Craft Commerce's ShippingMethodOption erases the information of any
custom ShippingMethod's that were used to create them. While not ideal,
this change sets a new field, shippingMethod on the ShippingMethodOption
class and also proxies the call to getShippingRules to use the attached
ShippingMethod if it is present.

Of course, other functions such as getName, getType, etc will not be
overridden and would need to be accessed like
$option->shippingMethod->getType().
@lukeholder
Copy link
Member

lukeholder commented Oct 17, 2023

Fixed in 531a4d3

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

"require": {
  "craftcms/commerce": "dev-develop#531a4d3c8019a87657b553d35273549d29acd7c2 as 4.3.0",
  "...": "..."
}

Then run composer update.

Thanks!

lukeholder added a commit that referenced this issue Oct 17, 2023
@nfourtythree
Copy link
Contributor

Commerce 4.3.1 has now been released with these changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug commerce4 Issues related to Commerce v4
Projects
None yet
3 participants