[v4 -> v5] Get the relevant shipping zone from the calculated shipping method for a cart during checkout #3637
-
In our v4 install that I am upgrading to v5 at the moment, we have a situation where we show various information about the zone on the shipping stage of checkout. There is information about restrictions on products / delivery embedded in the Shipping Zone description which we show to a user when their calculated shipping method relates to a zone that has such information. So in v4 we were able to do: {% for handle, method in cart.availableShippingMethodOptions %}
...
{% set rule = method.getMatchingShippingRule(cart) %}
{% if rule %}
{% set shipping_zone = craft.commerce.shippingZones.getShippingZoneById(rule.shippingZoneId) %}
{% if shipping_zone and shipping_zone.description %}
<br>{{ shipping_zone.name }}: {{ shipping_zone.description }}
{% endif %}
{% endif %}
...
{% endfor %} But in v5, this code fails, with the error:
Which refers to: {% set shipping_zone = craft.commerce.shippingZones.getShippingZoneById(rule.shippingZoneId) %} There is no longer a I have been going through the code and cannot work out how to get a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
My guess is your zone condition was migrated to the order shipping address zone condition builder? if so, you will need to get the condition builder and loop over the conditions and find its zone condition value. Something like this? (not tested) {% set condition = rule.orderCondition.conditionRules|filter(item => item is instance of ('craft\\commerce\\elements\\conditions\\orders\\ShippingAddressZoneConditionRule'))|first %}
{% if condition %}
Shipping zone IDs in this rule: {{ condition.getValues()|join(',') }}
{% else %}
{# No condition of the specified class was found #}
<p>No ShippingAddressZoneConditionRule found.</p>
{% endif %} in php something like this $rule = Plugin::getInstance()->getShippingRules()->getAllShippingRules()[0];
$condition = collect($rule->getOrderCondition()->getConditionRules())
->firstWhere(fn($item) => $item instanceof craft\commerce\elements\conditions\orders\ShippingAddressZoneConditionRule);
$zoneIds = $condition->getValues(); |
Beta Was this translation helpful? Give feedback.
My guess is your zone condition was migrated to the order shipping address zone condition builder?
if so, you will need to get the condition builder and loop over the conditions and find its zone condition value.
Something like this? (not tested)
in php something like this
$rule =…