You are here

function template_preprocess_uc_order_invoice in Ubercart 8.4

Preprocesses a formatted invoice with an order's data.

See also

uc_order--admin.tpl.php

uc_order--customer.tpl.php

File

uc_order/uc_order.module, line 343
Handles all things concerning Ubercart orders.

Code

function template_preprocess_uc_order_invoice(&$variables) {
  $order =& $variables['order'];
  switch ($variables['op']) {
    case 'checkout-mail':
      $variables['thank_you_message'] = TRUE;
    case 'admin-mail':
      $variables['help_text'] = TRUE;
      $variables['email_text'] = TRUE;
      $variables['store_footer'] = TRUE;
    case 'view':
    case 'print':
      $variables['business_header'] = TRUE;
      $variables['shipping_method'] = TRUE;
      break;
  }
  $variables['shippable'] = $order
    ->isShippable();
  $variables['products'] = [];
  if (!empty($order->products)) {
    foreach ($order->products as $id => $product) {
      $variables['products'][$id] = new \stdClass();
      $variables['products'][$id]->title = $product->title->value;
      $variables['products'][$id]->qty = $product->qty->value;
      $variables['products'][$id]->model = $product->model->value;
      $variables['products'][$id]->total_price = uc_currency_format($product->price->value * $product->qty->value);
      if ($product->qty->value > 1) {
        $variables['products'][$id]->individual_price = t('(@price each)', [
          '@price' => uc_currency_format($product->price->value),
        ]);
      }
      else {
        $variables['products'][$id]->individual_price = '';
      }
      $variables['products'][$id]->details = [];
      if (!empty($product->data->attributes)) {
        $attributes = [];
        foreach ($product->data->attributes as $attribute => $option) {
          $attributes[] = t('@attribute: @options', [
            '@attribute' => $attribute,
            '@options' => implode(', ', (array) $option),
          ]);
        }
        $variables['products'][$id]->details = [
          '#theme' => 'item_list',
          '#items' => $attributes,
        ];
      }
    }
  }
  $variables['line_items'] = $variables['order']
    ->getDisplayLineItems();
  $order->line_items = $variables['line_items'];

  // Generate tokens to use as template variables.
  $types = [
    'uc_order' => $order,
  ];
  $token_service = \Drupal::token();
  $token_info = $token_service
    ->getInfo();
  $bubbleable_metadata = new BubbleableMetadata();
  $replacements = [];
  foreach ([
    'site',
    'store',
    'uc_order',
  ] as $type) {
    $tokens = array_keys($token_info['tokens'][$type]);
    $replacements[$type] = $token_service
      ->generate($type, array_combine($tokens, $tokens), $types, [], $bubbleable_metadata);
  }

  // @todo Where do we apply the metadata to?
  // $bubbleable_metadata->applyTo($build);
  foreach ($replacements as $type => $tokens) {
    foreach ($tokens as $token => $value) {
      $key = str_replace('-', '_', $type . '_' . $token);
      $key = str_replace('uc_', '', $key);
      $variables[$key] = $value;
    }
  }

  // Add hook suggestions, default to customer template.
  if ($variables['template']) {
    $variables['theme_hook_suggestions'][] = 'uc_order_invoice__' . $variables['template'];
  }
}