<p>You can simplify your first bit of code by using:</p>

<p><code>{{ product.taxCategory.name == 'withVat' ? '+VAT' }}</code></p>

<p>The reason you get the error with your second bit of code is because <code>craft.commerce.taxCategories</code> is only an array of Tax Category names. Only the models of each Tax Category would have properties such as <code>.id</code> and <code>.name</code></p>

CC BY-SA 3.0
2016-07-13T16:40:43.493Z
2016-07-13T16:40:43.493Z
Luke Pearce
<p>A full <code>Commerce_TaxCategoryModel</code> array will be returned in Craft Commerce 1.2</p>

<p>If you would like this functionality now, please replace the <code>getTaxCategories()</code> function in the <code>plugins/commerce/variables/CommerceVariable.php</code> file with this replacement function:</p>

<pre><code>public function getTaxCategories()
{
    $taxCategories = craft()-&gt;commerce_taxCategories-&gt;getAllTaxCategories();

    // Need to put the methods into an array keyed by method ID for backwards compatibility.
    $newArray = [];
    foreach ($taxCategories as $model)
    {
        $newArray[$model-&gt;id] = $model;
    }

    return $newArray;
}
</code></pre>

CC BY-SA 3.0
2016-07-14T09:42:35.137Z
Ooh. Thanks Luke! Will wait until 1.2 and make a point to update my templates.
Clive Portman
CC BY-SA 3.0
2016-08-17T17:07:55.593Z
Any idea when 1.2 will be released @lukeholder?
Terry Upton
CC BY-SA 3.0
2016-07-14T02:32:40.147Z
2016-07-14T02:32:40.147Z
Luke Holder
<p>I'm trying to test for a taxCategory within Craft Commerce and finding it awkward to do so. I can't see much in the docs, but have managed to put this together:</p>

<pre><code>{% if product.taxCategory | length %}
    {% for taxCategory in product.taxCategory %}
        {# loops through the taxCategory with the first string being the id, #}
        {# the second the name and the third string seems to be the slug #}
        {% if loop.index == 3 and taxCategory == "withVat" %}
            +VAT
        {% endif %}
    {% endfor %}
{% endif %}
</code></pre>

<p>But it doesn't seem very elegant. Any suggestions? The docs example probably needs updating, I think, because:</p>

<pre><code>{% for taxCategory in craft.commerce.taxCategories %}
    {{ taxCategory.id }} - {{ taxCategory.name }}
{% endfor %}
{% endif %}
</code></pre>

<p>...outputs: "Impossible to access an attribute ("id") on a string variable"</p>

CC BY-SA 3.0
2016-07-07T15:25:28.140Z
ask luke holder on the craft commerce slack channel
Amit Erandole
<plugin-craftcommerce>
How to output taxCategory in Craft Commerce
# How to output taxCategory in Craft Commerce

(Asked by: username_0 on 2016-07-07)

I'm trying to test for a taxCategory within Craft Commerce and finding it awkward to do so. I can't see much in the docs, but have managed to put this together:
{% if product.taxCategory | length %}
    {% for taxCategory in product.taxCategory %}
        {# loops through the taxCategory with the first string being the id, #}
        {# the second the name and the third string seems to be the slug #}
        {% if loop.index == 3 and taxCategory == "withVat" %}
            +VAT
        {% endif %}
    {% endfor %}
{% endif %}

But it doesn't seem very elegant. Any suggestions? The docs example probably needs updating, I think, because:
{% for taxCategory in craft.commerce.taxCategories %}
    {{ taxCategory.id }} - {{ taxCategory.name }}
{% endfor %}
{% endif %}

...outputs: "Impossible to access an attribute ("id") on a string variable"

**Tags:** plugin-craftcommerce

> **Comments:**
> **username_1**: ask luke holder on the craft commerce slack channel  

---

## 2 answers

### Answer by username_2 on 2016-07-13. Score: 1 | Accepted answer

You can simplify your first bit of code by using:

`{{ product.taxCategory.name == 'withVat' ? '+VAT' }}`

The reason you get the error with your second bit of code is because `craft.commerce.taxCategories` is only an array of Tax Category names. Only the models of each Tax Category would have properties such as `.id` and `.name`


---

### Answer by username_3 on 2016-07-14. Score: 1

A full Commerce_TaxCategoryModel array will be returned in Craft Commerce 1.2
If you would like this functionality now, please replace the getTaxCategories() function in the plugins/commerce/variables/CommerceVariable.php file with this replacement function:
public function getTaxCategories()
{
    $taxCategories = craft()->commerce_taxCategories->getAllTaxCategories();

    // Need to put the methods into an array keyed by method ID for backwards compatibility.
    $newArray = [];
    foreach ($taxCategories as $model)
    {
        $newArray[$model->id] = $model;
    }

    return $newArray;
}

> **Comments:**
> **username_0**: Ooh. Thanks Luke! Will wait until 1.2 and make a point to update my templates.  
> **username_4**: Any idea when 1.2 will be released @lukeholder?  

---

