You are here

protected function TaxTypeBase::buildCustomerProfile in Commerce Core 8.2

Builds a customer profile for the given order.

Constructed only for the purposes of tax calculation, never saved. The address comes one of the saved profiles, with the following priority:

  • Shipping profile
  • Billing profile
  • Store profile (if the tax type is display inclusive)

The tax number comes from the billing profile, if present.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The order.

Return value

\Drupal\profile\Entity\ProfileInterface|null The customer profile, or NULL if not available yet.

1 call to TaxTypeBase::buildCustomerProfile()
TaxTypeBase::resolveCustomerProfile in modules/tax/src/Plugin/Commerce/TaxType/TaxTypeBase.php
Resolves the customer profile for the given order item.

File

modules/tax/src/Plugin/Commerce/TaxType/TaxTypeBase.php, line 270

Class

TaxTypeBase
Provides the base class for tax types.

Namespace

Drupal\commerce_tax\Plugin\Commerce\TaxType

Code

protected function buildCustomerProfile(OrderInterface $order) {
  $order_id = $order
    ->id();
  if (!isset($this->profiles[$order_id])) {
    $order_profiles = $order
      ->collectProfiles();
    $address = NULL;
    foreach ([
      'shipping',
      'billing',
    ] as $scope) {
      if (isset($order_profiles[$scope])) {
        $address_field = $order_profiles[$scope]
          ->get('address');
        if (!$address_field
          ->isEmpty()) {
          $address = $address_field
            ->getValue();
          break;
        }
      }
    }
    if (!$address && $this
      ->isDisplayInclusive()) {

      // Customer is still unknown, but prices are displayed tax-inclusive
      // (VAT scenario), better to show the store's default tax than nothing.
      $address = $order
        ->getStore()
        ->getAddress();
    }
    if (!$address) {

      // A customer profile isn't usable without an address. Stop here.
      return NULL;
    }
    $tax_number = NULL;
    if (isset($order_profiles['billing'])) {
      $tax_number = $order_profiles['billing']
        ->get('tax_number')
        ->getValue();
    }
    $profile_storage = $this->entityTypeManager
      ->getStorage('profile');
    $this->profiles[$order_id] = $profile_storage
      ->create([
      'type' => 'customer',
      'uid' => 0,
      'address' => $address,
      'tax_number' => $tax_number,
    ]);
  }
  return $this->profiles[$order_id];
}