<p>100% untested, but I think one way to go would be to use <a href="http://www.yiiframework.com/doc/api/1.1/CModel#addError-detail" rel="nofollow">addError</a> on one of the <code>Commerce_OrderModel</code>'s attributes (in the <code>defineAttributes</code> method).</p>

<p>Then your template could do something like:</p>

<pre><code>{% if 'myShippingMethod' not in craft.commerce.availableShippingMethods|keys %}
    {% for error in order.getErrors('theAttribute') %}
        {{ error }}&lt;br /&gt;
    {% endfor %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-10-11T22:23:12.233Z
Huh, that's a pretty clever idea. I'll run with it and see what I can do!
taylor
CC BY-SA 3.0
2016-10-11T22:29:01.540Z
Just tried it - worked perfectly. I love when a solution for something ends up being knowledge that'll help in many other areas down the road. Thanks Brad!
taylor
CC BY-SA 3.0
2016-10-11T22:30:57.103Z
Glad to help, @taylor!
Brad Bell
CC BY-SA 3.0
2016-10-11T22:03:41.637Z
2016-10-11T22:03:41.637Z
Brad Bell
<p>I have multiple custom shipping methods, each with rules which determine whether or not they will be available to the user. Basic things like minimum order cost and their address which would determine if a certain shipping method is available.</p>

<p>There are some scenarios where a shipping method wont be available and the reason could depend. I need to be able to tell the user what exactly caused it to be unavailable.</p>

<p>So for example:</p>

<pre><code>public function matchOrder(\Craft\Commerce_OrderModel $order){

    // If this is true, do not enable shipping method.
    if($scenario_1) { return false; }

    // If this is true, do not enable shipping method.
    if($scenario_2) { return false; }

    return true;

}
</code></pre>

<p>In the event that one of those is true, the shipping method is not available to the user. On the shipping page, I can check if it is enabled or not like this:</p>

<pre><code>{% if 'myShippingMethod' not in craft.commerce.availableShippingMethods|keys %}{% endif %}
</code></pre>

<p>But that only does me good in simply whether or not it's available - not for knowing why it isn't available. Is there a way I could somehow 'pass' information to the templates for the shipping methods that didn't return true? That way I could tell user "This shipping method isn't available because of <code>$scenario_1</code>" or "This shipping method isn't available due to <code>$scenario_2</code>."</p>

<p>Does this make sense? Trying to think of a good way to go about this. Any thoughts about how I should go about this would be super appreciated.</p>

<plugin-development><plugin-craftcommerce><php><shipping>
Custom Shipping Method Availability in Commerce - User Messages
# Custom Shipping Method Availability in Commerce - User Messages

(Asked by: username_0 on 2016-10-11)

I have multiple custom shipping methods, each with rules which determine whether or not they will be available to the user. Basic things like minimum order cost and their address which would determine if a certain shipping method is available.
There are some scenarios where a shipping method wont be available and the reason could depend. I need to be able to tell the user what exactly caused it to be unavailable.
So for example:
public function matchOrder(\Craft\Commerce_OrderModel $order){

    // If this is true, do not enable shipping method.
    if($scenario_1) { return false; }

    // If this is true, do not enable shipping method.
    if($scenario_2) { return false; }

    return true;

}

In the event that one of those is true, the shipping method is not available to the user. On the shipping page, I can check if it is enabled or not like this:
{% if 'myShippingMethod' not in craft.commerce.availableShippingMethods|keys %}{% endif %}

But that only does me good in simply whether or not it's available - not for knowing why it isn't available. Is there a way I could somehow 'pass' information to the templates for the shipping methods that didn't return true? That way I could tell user "This shipping method isn't available because of $scenario_1" or "This shipping method isn't available due to $scenario_2."
Does this make sense? Trying to think of a good way to go about this. Any thoughts about how I should go about this would be super appreciated.

**Tags:** plugin-development, plugin-craftcommerce, php, shipping

---

## 1 answers

### Answer by username_1 on 2016-10-11. Score: 1 | Accepted answer

100% untested, but I think one way to go would be to use addError on one of the Commerce_OrderModel's attributes (in the defineAttributes method).
Then your template could do something like:
{% if 'myShippingMethod' not in craft.commerce.availableShippingMethods|keys %}
    {% for error in order.getErrors('theAttribute') %}
        {{ error }}
    {% endfor %}
{% endif %}

> **Comments:**
> **username_0**: Huh, that's a pretty clever idea. I'll run with it and see what I can do!  
> **username_0**: Just tried it - worked perfectly. I love when a solution for something ends up being knowledge that'll help in many other areas down the road. Thanks Brad!  
> **username_1**: Glad to help, @taylor!  

---

