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

Optionally allow a shippingMethod to be associated with a ShippingMethodOption #3274

Conversation

johnnynotsolucky
Copy link
Contributor

Description

Craft Commerce's ShippingMethodOption erases the information of any custom ShippingMethods 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().

With this change the following snippet works with Commerce built-ins and custom shipping methods:

{% set cart = craft.orders().one() %}

{% for handle, method in cart.availableShippingMethodOptions %}
  <div class="shipping-select">
    <label>
      <input type="radio" name="shippingMethodHandle" value="{{ handle }}" {% if handle == cart.shippingMethodHandle %}checked{% endif %} />
      <strong>{{ method.name }}</strong>

      <span class="price">
          {{ method.getPrice() | commerceCurrency(cart.currency) }}
      </span>

      {% set shippingRules = method.getShippingRules() %}
      <code>{{ dump(shippingRules) }}</code>
    </label>
  </div>
{% endfor %}

Related issues

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

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 lukeholder closed this Oct 17, 2023
@lukeholder
Copy link
Member

Decided to let you do option.shippingMethod.getShippingRules() since options are not shipping methods and not all shipping methods have rules. We plan to clean up this interface in the future.

@johnnynotsolucky johnnynotsolucky deleted the custom-shipping-methods branch October 17, 2023 10:14
@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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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