<p>You haven't executed your category query. Add an execution method to the end of the query, such as <code>.all()</code> or <code>.one()</code>. <a href="https://craftcms.com/docs/3.x/element-queries.html#executing-element-queries" rel="nofollow noreferrer">https://craftcms.com/docs/3.x/element-queries.html#executing-element-queries</a></p>

CC BY-SA 4.0
2022-04-21T11:56:44.250Z
I can't believe my very eyes !!!! :D 
I'm good with PHP but obviously lack a few (a lot) notions of twig !
I think I tried different things, like variant.myObject.find() and craft.categories.group... without success... 
Sure enough YOUR SOLUTION WORKS JUST FINE !
Thanks again, a thank you for all of us sharing your knowledge to make other's life simplier ! :D
Sylvain Tremblay
CC BY-SA 4.0
2022-04-21T07:40:09.557Z
2022-04-21T07:40:09.557Z
James Smith
<p>In Products (Craft Commerce), I've set custom fields. Some are texts, some are related to a Category. Actually, there's 2 different categories per variant...</p>
<p>In order to draw the  fields, I need to retrieve each category of each variant.
<a href="https://i.sstatic.net/uWTn4.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/uWTn4.png" alt="enter image description here" /></a></p>
<p>Looping thru variants to show text fields is no brainer, but I can't manage to get any category object.</p>
<p>Here's the variant definition:</p>
<p><a href="https://i.sstatic.net/cASx1.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/cASx1.png" alt="enter image description here" /></a></p>
<p>And the code I put to show what's going on.</p>
<pre><code>{% for variant in product.getVariants() %}
  {{ variant.id }} - {{ variant.sku }} - {{ variant.vendorLength }}
{# This show a CategoryQuery Object #}
  {{ variant.attributeGrainWay }}&lt;br&gt;&lt;hr&gt;

{# This show a CategoryQuery Object as well... #}
{% set abc = craft.categories.group('woodGrainOrientation').relatedTo(variant.attributeGrainWay) %}
&lt;pre&gt;{{ abc }}&lt;/pre&gt;
</code></pre>
<p>No matter what, I can't manage to get what I need.
Here's a sample output.
<a href="https://i.sstatic.net/Lb1aZ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Lb1aZ.png" alt="enter image description here" /></a></p>
<p>As always, your help is greatly appreciated.</p>
<p>Sylvain</p>

<plugin-craftcommerce><category><products><variants>
Category as variant
# Category as variant

(Asked by: username_0 on 2022-04-20)

In Products (Craft Commerce), I've set custom fields. Some are texts, some are related to a Category. Actually, there's 2 different categories per variant...
In order to draw the  fields, I need to retrieve each category of each variant.

Looping thru variants to show text fields is no brainer, but I can't manage to get any category object.
Here's the variant definition:

And the code I put to show what's going on.
{% for variant in product.getVariants() %}
  {{ variant.id }} - {{ variant.sku }} - {{ variant.vendorLength }}
{# This show a CategoryQuery Object #}
  {{ variant.attributeGrainWay }}

{# This show a CategoryQuery Object as well... #}
{% set abc = craft.categories.group('woodGrainOrientation').relatedTo(variant.attributeGrainWay) %}
{{ abc }}

No matter what, I can't manage to get what I need.
Here's a sample output.

As always, your help is greatly appreciated.
Sylvain

**Tags:** plugin-craftcommerce, category, products, variants


---

## 1 answers

### Answer by username_1 on 2022-04-21. Score: 1 | Accepted answer

You haven't executed your category query. Add an execution method to the end of the query, such as `.all()` or `.one()`. <https://craftcms.com/docs/3.x/element-queries.html#executing-element-queries>

> **Comments:**
> **username_0**: I can't believe my very eyes !!!! :D  I'm good with PHP but obviously lack a few (a lot) notions of twig ! I think I tried different things, like variant.myObject.find() and craft.categories.group... without success...  Sure enough YOUR SOLUTION WORKS JUST FINE ! Thanks again, a thank you for all of us sharing your knowledge to make other's life simplier ! :D  

---

