I have a product type called gift with 3 different variations.

I add an attribute to it so that a user can choose a custom variation based on the attribute, attribute (A, B, C).

I also have a custom checkout pane, If a user choose the first variation (by choosing the attribute), i can access the variation that is selected. for example by using 2 lines of code below:

$variations = $gift->variations->getEntity();



But if a user choose another variations except the first variation (for example: variation B), I can not access to that variation.

public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
    $pane_form['#order_entity'] = $this->order;

    $order_items = $this->order->getItems();
    /** @var \Drupal\commerce_order\Entity\OrderItem $item */
    $item = $order_items[0];
    $gift = $item->getPurchasedEntity();
    $gift = Product::load($gift->id());

    $variations = $gift->variations->getEntity(); // Just works for first variation
    drupal_set_message('bundle: ' . $variations->bundle());

    // This line of code works for just first variation too
    // $id = $gift->variations->target_id;


  • Hi and welcome. It is unclear what you want. Is this a bug report, or you you want to know how to access the variation? Could you please phrase a question? Jul 11, 2017 at 13:05
  • What does $gift->variations[1]->getEntity() get you?
    – Clive
    Jul 11, 2017 at 13:23
  • If @Clive is right, I suppose $gift->variations->referencedEntities(); should also return an array of entities. Jul 11, 2017 at 13:27
  • 1
    @Neograph734, I was wrong and thought that it's a bug. Bojanz corrected and told me $item->getPurchasedEntity() is the product variation, not the product. link.
    – Iman Kiani
    Jul 15, 2017 at 6:34

2 Answers 2


Basically, I wanted to get attributes attached to the current variation. According to Bojanz' answer: https://www.drupal.org/node/2893842#comment-12163314, there was no need to use $gift->variations. Bojanz corrected and told me $item->getPurchasedEntity() is the product variation, not the product

So the code would be:

   * {@inheritdoc}
  public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
    $pane_form['#order_entity'] = $this->order;

    $order_items = $this->order->getItems();
    /** @var \Drupal\commerce_order\Entity\OrderItem $item */
    $item = $order_items[count($order_items) - 1]; // the last item added to the order
    /** @var \Drupal\commerce_product\Entity\ProductVariation $gift */
    $gift = $item->getPurchasedEntity();
    $attribute = $gift->getAttributeValue('attribute_gift_type');

You have been using the commerce API so well, except for the last step. If your gift is a Drupal\commerce_product\Entity\Product entity, it means that it implements Drupal\commerce_product\Entity\ProductInterface.

This interface already has an function for accessing product variations: getVariations().

You should be able to use $variations = $gift->getVariations(); to obtain an array of product variations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.