Products Fields

Products fields allow you to relate products to a parent element.

Settings #

Products Field Settings

Products fields have the following settings:

  • Sources – The product types you want to relate entries from. (Default is “All”.)
  • Limit – The maximum number of products that can be related with the field at once. (Default is no limit.)
  • Selection Label – The label that should be used on the field’s selection button. (Default is "Add a product")

The Field #

Products fields list all of the currently selected products, with a button to select new ones:

Product Field Example

Clicking the “Add an entry” button will bring up a modal window where you can find and select additional entries:

Product Field Modal

Templating #

If you have an element with a Products field in your template, you can access its selected products using your Product field’s handle:

{% set products = entry.productsFieldHandle %}

That will give you an ElementCriteriaModel object, prepped to output all of the selected roductsp for the given field. In other words, the line above is really just a shortcut for this:

{% set products = craft.commerce.products({
    relatedTo: { sourceElement: entry, field: "productsFieldHandle" },
    order:     "sortOrder",
    limit:     null
}) %}

(See Relations for more info on the relatedTo param.)

Examples #

To check if your Products field has any selected products, you can use the length filter:

{% if entry.productsFieldHandle | length %}
    ...
{% endif %}

To loop through the selected products, you can treat the field like an array:

{% for product in entry.productsFieldHandle %}
    ...
{% endfor %}

Rather than typing “entry.productsFieldHandle” every time, you can call it once and set it to another variable:

{% set products = entry.productsFieldHandle %}

{% if products | length %}

    <h3>Some great products</h3>
    {% for product in products %}
        ...
    {% endfor %}

{% endif %}

You can add parameters to the ElementCriteriaModel object as well:

{% set clothingProducts = entry.productsFieldHandle.type('clothing') %}

If your Products field is only meant to have a single product selected, remember that calling your Products field will still give you the same ElementCriteriaModel, not the selected product. To get the first (and only) product selected, use first():

{% set product = entry.productsFieldHandle.first() %}

{% if product %}
    ...
{% endif %}

Buy Craft Commerce

Craft Commerce site license

Includes

  • Developer support
  • One year of updates ($199/year for updates after that)
  • Craft Pro site license
Buy now for $999
Can I try before buying?

Absolutely. Just download it from the “Download” button in the site header and follow our installation instructions. You’ll have the full feature set to experiment with, without any restrictions, as long as you’re running it from a development, testing, or staging domain.

Will there be recurring/upgrade fees?

Each Commerce license comes with one year of updates. After that, you can unlock additional updates for $199 per year.

Can I use this on an existing Craft site?

Yes! And if you already have a Craft Pro license (or you don’t need one), you can purchase Craft Commerce directly from the Plugin Store or just leave the switch off that says “Include a Craft Pro license” in the purchase form.

Is Craft Pro required?

Craft Commerce does not require Craft Pro; it works perfectly fine on Craft Solo as well!

Can I try before buying?

Absolutely. Just download it from the “Download” button in the site header and follow our installation instructions. You’ll have the full feature set to experiment with, without any restrictions, as long as you’re running it from a development, testing, or staging domain.

Will there be recurring/upgrade fees?

Each Commerce license comes with one year of updates. After that, you can unlock additional updates for $199 per year.

Can I use this on an existing Craft site?

Yes! And if you already have a Craft Pro license (or you don’t need one), you can purchase Craft Commerce directly from the Plugin Store or just leave the switch off that says “Include a Craft Pro license” in the purchase form.

Is Craft Pro required?

Craft Commerce does not require Craft Pro; it works perfectly fine on Craft Solo as well!