<p>It's a bit confusing, but since this product type has variants, using this should work:</p>

<pre><code>shop/{object.categories.first().slug}/{object.slug}
</code></pre>

<p>If it didn't have variants, you wouldn't need the <code>object</code> reference in front of it.  The same thing applies if you're referencing a field inside of Matrix as well.</p>

<p>This has been made easier in the upcoming Commerce 2 so you don't have to worry about the differences.</p>

CC BY-SA 3.0
2017-10-26T00:14:11.123Z
2017-10-26T00:14:11.123Z
Brad Bell
<p>I'm in the middle of learning Craft Commerce.  When I go to an individual product my URL says <code>/shop/wella-shampoo/</code>.  I'd like it to say <code>/shop/**hair-care**/wella-shampoo/</code> because this is the category this product falls into.</p>
<p>I've tried adding <code>shop/{categories.first().slug}/{slug}</code> to the URL Format but I get the following error:</p>
<blockquote>
<p>Internal Server Error</p>
<p>Craft\Commerce_ProductModel and its behaviors do not have a method or closure named &quot;categories&quot;.</p>
</blockquote>

CC BY-SA 3.0
2017-10-20T23:14:16.167Z
Is `categories` the name of a Category Field Type on your Product Type's Field Layout?
Brad Bell
CC BY-SA 3.0
2017-10-21T09:24:09.693Z
Hi, no the field is called 'assignToCategory'?
Martin
CC BY-SA 3.0
2017-10-21T15:22:19.020Z
And does this particular product have variants or no?
Brad Bell
CC BY-SA 3.0
2017-10-23T13:38:38.057Z
Yes, the product has variants
Martin
<plugin-craftcommerce><categories><url>
Get the category into the url of the product page
# Get the category into the url of the product page

(Asked by: username_0 on 2017-10-20)

I'm in the middle of learning Craft Commerce. When I go to an individual product my URL says `/shop/wella-shampoo/`. I'd like it to say `/shop/**hair-care**/wella-shampoo/` because this is the category this product falls into.

I've tried adding `shop/{categories.first().slug}/{slug}` to the URL Format but I get the following error:

> Internal Server Error
>
> Craft\Commerce\_ProductModel and its behaviors do not have a method or closure named "categories".

**Tags:** plugin-craftcommerce, categories, url
> **Comments:**
> **username_1**: Is `categories` the name of a Category Field Type on your Product Type's Field Layout?  
> **username_0**: Hi, no the field is called 'assignToCategory'?  
> **username_1**: And does this particular product have variants or no?  
> **username_0**: Yes, the product has variants  

---

## 1 answers

### Answer by username_1 on 2017-10-26. Score: 1

It's a bit confusing, but since this product type has variants, using this should work:
shop/{object.categories.first().slug}/{object.slug}

If it didn't have variants, you wouldn't need the object reference in front of it.  The same thing applies if you're referencing a field inside of Matrix as well.
This has been made easier in the upcoming Commerce 2 so you don't have to worry about the differences.


---

