Product attributes and values

Product variation types can have certain attributes (ex. color) and those attributes have values (ex red, blue). In this example, we will create two attributes (color and size) and add them to the variation type we made previously.

Creating attributes

     * id [String]
     *   The primary key for this attribute.
     * label [String]
     *   The label for this attribute.
    $color_attribute = \Drupal\commerce_product\Entity\ProductAttribute::create([
      'id' => 'color',
      'label' => 'Color',

    $size_attribute = \Drupal\commerce_product\Entity\ProductAttribute::create([
      'id' => 'size',
      'label' => 'Size',

    // We load a service that adds the attributes to the variation type we made previously.
    $attribute_field_manager = \Drupal::service('commerce_product.attribute_field_manager');

    $attribute_field_manager->createField($color_attribute, 'my_custom_variation_type');
    $attribute_field_manager->createField($size_attribute, 'my_custom_variation_type');

Loading an attribute

    // Loading is based off of the primary key [String] that was defined when creating it.
    $size_attribute = \Drupal\commerce_product\Entity\ProductAttribute::load('size');

Checking if an attribute value exists within a particular attribute type

    // Look up while filtering by Attribute
    $productAttributeId = \Drupal::entityTypeManager()
          ->condition('attribute', 'attribute_machine_name')
          ->condition('field_value', field_value)

Creating values for an attribute

     * attribute [String]
     *   Foreign key of the attribute we want.
     * name [String]
     *   The name of this value.
    $red = \Drupal\commerce_product\Entity\ProductAttributeValue::create([
      'attribute' => 'color',
      'name' => 'Red',

    $blue = \Drupal\commerce_product\Entity\ProductAttributeValue::create([
      'attribute' => 'color',
      'name' => 'Blue',

    $medium = \Drupal\commerce_product\Entity\ProductAttributeValue::create([
      'attribute' => 'size',
      'name' => 'Medium',

    $large = \Drupal\commerce_product\Entity\ProductAttributeValue::create([
      'attribute' => 'size',
      'name' => 'Large',

Loading an attribute value

    // Loading is based off of the primary key [Integer]
    //   1 would be the first one saved, 2 the next, etc.
    $red = \Drupal\commerce_product\Entity\ProductAttributeValue::load(1);

Assigning attributes to a variation

Let's say we want our hypothetical product to have two variations. One will be the color red and size medium, and the other will be the color blue and size large. // [IMPORTANT] - If a Product Variation Type has fields for attributes (as we added above), then variations of that type MUST have those attributes.

     * attribute_<ATTRIBUTE_ID> [\Drupal\commerce_product\Entity\ProductAttributeValueInterface]
     *   The attribute value entity to use for the attribute type.
    $variation_red_medium = \Drupal\commerce_product\Entity\ProductVariation::create([
      'type' => 'my_custom_variation_type',
      'sku' => 'product-red-medium',
      'price' => new \Drupal\commerce_price\Price('10.00', 'USD'),
      'attribute_color' => $red,
      'attribute_size' => $medium,

    $variation_blue_large = \Drupal\commerce_product\Entity\ProductVariation::create([
      'type' => 'my_custom_variation_type',
      'sku' => 'product-blue-large',
      'price' => new \Drupal\commerce_price\Price('10.00', 'USD'),
      'attribute_color' => $blue,
      'attribute_size' => $large,

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