0

I am trying to get variation type field values from the order entity but I can't find any documentation on this. I am using an event subscriber to run the following code when an order is paid. The code works, but I can't see any reference to the fields in kint and I can't find any references to any kind of "getVariation" or equivalent function online.

/**
 * This method is called when the commerce_order.order.paid is dispatched.
 *
 * @param \Symfony\Component\EventDispatcher\Event $event
 *   The dispatched event.
 */
public function commerceOrderOrderPaid(Event $event) {
  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $event->getOrder();
  $items = $order->getItems();
  foreach ($items as $item) {
    kint($item);
    if ($item->get('type')->target_id == 'purchase_things') {
      // 'number_of_things' is a variation type field.
      $thingQuant = $item->get('number_of_things'); 
      // The line does not work, obviously. 
      // I cannot see 'number_of_things' anywhere in kint($item).
    }
  }
}

1 Answer 1

1

Using Devel and Kint I was able to find the "getVariation" function I was looking for. This code lets you get the variation type fields.

$item->getPurchasedEntity()->get('field_number_of_things')->value

Your Answer

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

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