1

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".

4
  • Is categories the name of a Category Field Type on your Product Type's Field Layout?
    – Brad Bell
    Commented Oct 20, 2017 at 23:14
  • Hi, no the field is called 'assignToCategory'?
    – Martin
    Commented Oct 21, 2017 at 9:24
  • And does this particular product have variants or no?
    – Brad Bell
    Commented Oct 21, 2017 at 15:22
  • Yes, the product has variants
    – Martin
    Commented Oct 23, 2017 at 13:38

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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