1

I'm using Drupal 8 with the module Commerce and I have an issue with the customer address.

I'm developing an export custom module in D8, and I'm stuck trying to load the profile (to access billing and shipping address) of a customer.

I'm not able to access this data from the custom module. And there is no documentation about the address pluggin ( https://www.drupal.org/project/address )

I have tried to load the order and the user data but there is no relevant field that helps me to achieve this.

Can anyone give me a hint?

Thanks in advance.

4 Answers 4

2

you can try get profile by order and can try the code below:

$order_id =1;
$entityManager = \Drupal::entityTypeManager();
$order = $entityManager->getStorage('commerce_order')->load($order_id);

get address by field billing_profile

$address = $order->billing_profile->entity->address->getValue();
1

Since the customer profile is a content entity you can access customer profile information by using the entityTypeManager() function.

$entity_manager = \Drupal::entityTypeManager();
$profile = $entity_manager->getStorage('profile')->load($profile_id);
1
  • 3
    It's worth noting that if you don't know the profile id and you're looking for the user profiles, try using $entity_manager->getStorage('profile')->loadByUser($account, 'customer', TRUE); and there's also loadMultipleByUser() and loadDefaultByUser(). Commented Jun 21, 2018 at 16:18
1

Starting from the order, this may help you:

if ($this->order->hasField('shipments') && !$this->order->get('shipments')->isEmpty() ) {
  $order_shipments = $this->order->get('shipments')->referencedEntities();
  $order_shipment = reset($order_shipments);
  $shipment_profile = $order_shipment->getShippingProfile();
  
  // Prepare shipment information.
  $addressValues = $shipment_profile->address->getValue();
  $shipment['locality'] = $addressValues[0]['locality'];
  $shipment['administrative_area'] = $addressValues[0]['administrative_area'];
  $shipment['postal_code'] = $addressValues[0]['postal_code'];
}
0

customer profile is a entity type and can be loaded by profile_id by using the entityTypeManager()

$entity_manager = \Drupal::entityTypeManager();
$profile = $entity_manager->getStorage('profile')->load($profile_id);

if you don't know the profile_id in the first place It can be also be loaded by properties using loadByProperties() method

$values = [
            'field_phone' => "123456789",
            "address" => [
                "country_code" => "BD",
            ]
        ];
$profile = \Drupal::entityTypeManager()->getStorage('profile')->loadByProperties($values);

Following fields are available for filtering for the profile

      "revision_id"
      "type"
      "uuid"
      "status"
      "uid"
      "is_default"
      "data"
      "created"
      "changed"
      "revision_created"
      "revision_user"
      "revision_log_message"
      "revision_default"
      "isDefaultRevision"
      "address"
      "field_phone"

last field is field_phone is created from cms which located in Profile config page (/admin/config/people/profile-types/manage/customer/fields).

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.