1

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"

  • ask luke holder on the craft commerce slack channel – Amit Erandole Jul 7 '16 at 15:25
1

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

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;
}
  • Ooh. Thanks Luke! Will wait until 1.2 and make a point to update my templates. – Clive Portman Jul 14 '16 at 9:42
  • Any idea when 1.2 will be released @lukeholder? – Terry Upton Aug 17 '16 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.