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.

Querying Variants

If a VariantAttributesField is attached to a product types variants, then it is possible to use that field to filter variants by the attributes and options stored in that field.

The Variant Attributes field accepts 3 formats of filters:

  • Filter by an option value, regardless of attribute;
  • Filter all provided attribute/option pairs;
  • And, filter by any of the provided attribute/option pairs.

Filter by an option value#

The following will return all variants which have the option 'Value':

PHP#

\craft\commerce\elements\Variant::find()->myVariantAttributes('Value')->all();

Twig#

{% craft.variants().myVariantAttributes("Value").all() %}

Filter by all attribute/option pairs#

The following will return all variants which have an attribute of Attribute A with a value of Value A1, and an attribute of Attribute B with a value of Value B1.

PHP

\craft\commerce\elements\Variant::find()->myVariantAttributes([
  'Attribute A' => 'Value A1',
  'Attribute B' => 'Value B1',
])->all();

Twig

{% set filter = {
  'Attribute A': 'Value A1',
  'Attribute B': 'Value B1',
} %}
{% set variants = craft.variants().myVariantAttributes(filter).all() %}

Filter by any attribute/option pairs#

The following will return all variants which have an attribute of Attribute A with a value of Value A1, or any option value of Value, or an attribute of Attribute B with a value of Value B1.

PHP

\craft\commerce\elements\Variant::find()->myVariantAttributes([
  ['Attribute A' => 'Value A1'],
  'Value',
  ['Attribute B' => 'Value B1'],
])->all();

Twig

{% set filter = {
  {'Attribute A': 'Value A1'},
  'Value',
  {'Attribute B': 'Value B1'},
} %}
{% set variants = craft.variants().myVariantAttributes(filter).all() %}

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