Product categories

In the Simple product type documentation, we added a custom field to a product variation type, to allow product variations to have images. In the Product type with attributes documentation, we added a custom field to a product attribute, to allow a Color attribute to be displayed as a color swatch image instead of just the color name. In this section, we're going to look at how to add fields to product types so that we can organize products into categories that can be used for the product catalog.

Prerequisites

Create a basic product category

Let's start by creating a "Brands" taxonomy to organize products by brand.

  1. Navigate to the "Taxonomy" page at /admin/structure/taxonomy.
  2. Click the "Add vocabulary" button.
  3. Enter "Brands" for the Name.
  4. Click "Save" to create the new vocabulary.

Create Brands vocabulary

Now you can add terms to the vocabulary or look into using the Feeds module or Migrate Tools module to import your pre-existing data.

Create a hierarchical product category

(need to add in some links to this section) For our product catalog, we're also going to want to hierarchical categories so that customers can use faceted search to "drill down" to specific categories. For example, on the Commerce Demo site, we have an "Urban Living" product category with sub-categories of "Food & Beverage", "Home", "Outdoors", etc. And within the "Home" category, we further classify products as "Accessories", "Kitchen", "Office", or "Furnishings".

To create a hierarchical taxonomy in Drupal, we can use the same steps we used to create a "basic" taxonomy. After creating a "Product categories" taxonomy and adding the terms, we have a listing that looks like this: Product categories taxonomy terms

Notice that the "Accessories" term appears to be duplicated, but that's fine, since one of those terms will be a sub-category of the Urban Living "Home" category, and the other will be a sub-category of the Urban Living "Outdoors" category. They have the same label but are actually two completely separate terms.

To transform this simple listing into a hierarchical taxonomy, you just need to use the drag-and-drop icons in the leftmost column to rearrange the terms. You may also find it helpful to click the "Show row weights" link to set the order of terms within a particular category, especially if there are a great number of items. After moving items around, the Product categories taxonomy now looks like this and all we need to do is click the "Save" button to save the changes:

Product categories hierarchy

Create a term reference field for your product category

Let's add a field that references our "Brands" taxonomy to both our Simple product type.

  1. Navigate to the "Proudct types" page at '/admin/commerce/config/product-types'.
  2. Select the "Manage fields" operation for the Simple product type.
  3. Click the "Add field" button.

Create new field

  1. Select "Taxonomy term" (in the "Reference" section) for the new field type.
  2. Enter "Brand" for the Label.
  3. Click the "Save and continue" button.

Add taxonomy term field

  1. Click "Save field settings" on the next page to save the default configuration values.
  2. On the "Brand settings for Simple" page, select "Required field" and select "Brands" for the Vocabulary.
  3. Click the "Save settings" button.

Brand settings

The "Brand" field has now been added to the Simple product type. To add the Brand field to another product type, you can "Re-use an existing field" to select this Brand field on the "Add field" page for the product type.

We can also create a new Taxonomy term reference field for our "Product categories" taxonomy and any other product taxonomies we want to create. Unlike the "Brand" field, which was limited to a single value, we'll specify "Unlimited" for the "Allowed number of values" for product categories:

Categories field settings

(Link to pages like faceted search/product catalog, etc. for "how to use" these taxonomies.)

Links and resources


In the next section, we'll look at options for product architectures in Drupal Commerce.

Found errors? Think you can improve this documentation? edit this page