5

What is the best method to organise products, and subsequently the order of these products within a group?

Currently I have setup a product type of Canine. I can access each of these products via a URL of /canine/productslug/ but I am unable to change the order of these products when viewed at /commerce/products/.

The shop will consist of two sections Canine and Equine. I don't think I need to have two product types as they contain the same structure/layout/format. So I can perhaps change the product type to general.

However, being new to Craft, I am unsure how I should be structuring these products? Presumably I need to create either categories or structures and then assign products to these?

I have done some searching but can't find anything to direct me over how I should be setting up the shop for product listings.

Can anyone offer some direction, advise or pointers for this?

1 Answer 1

7

Product types are really all about data concerns - so if the data you're storing for your canines/equines (hereafter known as dogs and horses to save some typing ;) ) - is the same, then you're right you don't need more than one product type. So let's say you make one type of product Animal.

The most obvious tool for grouping that Craft provides is categories. You might make a category group of Animal Categories, and then two categories in that Dogs and Horses. You then add a categories field to your product type and select that category group as the source.

Then, when entering each product, you just choose the appropriate category at that time (i.e. you don't so much assign product to a category from the category itself, rather the product is related to the category using the field on the product entry).

In terms of then listing those on a listing (rather than individual) page, in the category settings, set your categories to resolve to e.g. animals/{slug} (i.e. an actual url might be animals/dogs and define a template for Craft to use.

When someone hits that url, your chosen template will be loaded, and this template will have an auto-populated variable of category you can then use that to filter your products display

{% set products = craft.commerce.product.relatedTo(category) %}
{% for product in products %}
    etc.

That's the basics of doing a listing

To sort orders, you can do it in any normal Craft way, such as by the title in alphabetic order:

{% set products = craft.commerce.product.relatedTo(category).order('title asc') %}

If you want an explicit order, you can do it at the template level by ids and used fixedOrder('true) and so on. There is currently no GUI method of sorting products in Commerce.

(It is fair to say though that it would be nice to have a native ability to create a default sort order for products in the back end, by e.g. drag and drop, which does not exist currently. You can achieve this by using a Commerce Products field (which is really a relations field, just to products) - and simply manually sort all your product in that, and use that field to build your listings, but this is kind of frustrating to be honest - as where you then store that field? We use a global set for this currently but it's not ideal).

7
  • Thanks for the answer Jeremy. I am glad you said it isn't possible to order products currently via the GUI, as I thought I was just overlooking something. Hope this would be a feature that might arrive in the future. As for the last part about manually sort all your products via Commerce Products field and global set. Could you elaborate a little on this as I don;t fully understand I am afraid. Jul 22, 2016 at 11:03
  • As the shop only has a few products and these are based in equine and canine categories explicitly, I wondered if categories was necessary and if having two product types (allbeit they are the same) would work? But you think it would still be better to use Categories for this? Jul 22, 2016 at 11:03
  • I'm on my phone but I suppose you could use types, although I can't see much benefit, but sure - either way should work. I guess I like to go with the natural way in case needs change later. OK create a global set called whatever. Then create a field of type commerce products. Add this to your global. You can then use this to order your products (try it!). Then if you retrieve these in your template you'll get them in the order they are in the firkd, by default. I can add more tomorrow if need be, but Friday night beckons.... Jul 22, 2016 at 11:11
  • Might be worth adding this as a feature request? Jul 22, 2016 at 11:36
  • It's already there actually - feedback.craftcms.com/forums/295386-commerce/suggestions/… - vote if you have an interest! Jul 23, 2016 at 4:15

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.