Skip to content

Commerce Foundations is our pre-built Craft Commerce store. See what it includes

Craft CMS plugins

Variant Manager

A Craft CMS plugin that imports and exports Craft Commerce product variants from CSV files.

Template tags

Twig helpers Variant Manager exposes on the craft variable for use in storefront templates.

craft.variantManager.getAttributeOptions(product, only?)#

Returns the distinct attribute names and the unique values used across a product’s variants. Useful for building variant pickers and faceted filters.

Parameters:

  • product: a Product element or a product ID.
  • only (optional): a string or array of attribute names to limit the result to.

Returns an array of associative arrays, each with:

  • name: the attribute name.
  • values: a deduplicated array of every value used by any variant for that attribute.

Example output#

{% set attributeOptions = craft.variantManager.getAttributeOptions(product.id) %}
{{ attributeOptions | json_encode }}

Renders something like:

[
  { "name": "Color", "values": ["Red", "Blue"] },
  { "name": "Size", "values": ["Small", "Medium", "Large"] }
]

Example: build a radio picker for every attribute#

{% set product = craft.products().id(30).one() %}

{% for attribute in craft.variantManager.getAttributeOptions(product) %}
  <fieldset>
    <legend>{{ attribute.name }}</legend>
    {% for value in attribute.values %}
      <label>
        <input type="radio" name="{{ attribute.name|kebab }}" value="{{ value }}">
        {{ value }}
      </label>
    {% endfor %}
  </fieldset>
{% endfor %}

Example: limit to specific attributes#

{% set colorsAndSizes = craft.variantManager.getAttributeOptions(product, ['Color', 'Size']) %}

Pass a single string for one attribute:

{% set colors = craft.variantManager.getAttributeOptions(product, 'Color') %}

We can take it from here.

We take on the Craft and Craft Commerce sites you built, so you can get back to building. Introduce a client who signs a management contract and you get a $3,000 partnership fee.

How the Dev Partnership Program works
See if we’re a fit