Can not access to the nth product's variation ( n > 1) programmatically 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: 

&lt;?php
$variations = $gift-&gt;variations-&gt;getEntity();
?&gt;


Or 

&lt;?php
$gift-&gt;variations-&gt;target_id
?&gt;


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

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

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

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

    // This line of code works for just first variation too
    // $id = $gift-&gt;variations-&gt;target_id;
}
?&gt;


Regards
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-&gt;variations. Bojanz corrected and told me $item-&gt;getPurchasedEntity() is the product variation, not the product

So the code would be: 

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

    $order_items = $this-&gt;order-&gt;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-&gt;getPurchasedEntity();
    $attribute = $gift-&gt;getAttributeValue('attribute_gift_type');
  }