You are here

static function CommerceGoogleTagManagerHelper::getOrderData in Commerce Google Tag Manager 7.2

Gets an array-based representation of the given Order.

Parameters

EntityMetadataWrapper $order The Order object:

Return value

array

1 call to CommerceGoogleTagManagerHelper::getOrderData()
CommerceGoogleTagManagerActionPurchase::execute in includes/rules/CommerceGoogleTagManagerActionPurchase.php
Executes the action.

File

includes/CommerceGoogleTagManagerHelper.php, line 42
This file contains the helper class CommerceGoogleTagManagerHelper.

Class

CommerceGoogleTagManagerHelper
@file This file contains the helper class CommerceGoogleTagManagerHelper.

Code

static function getOrderData(\EntityMetadataWrapper $order, $event = NULL) {
  $tax_sum = 0;
  if (module_exists('commerce_tax')) {
    foreach (commerce_tax_rates() as $name => $tax_rate) {
      if ($tax_rate['price_component']) {
        $tax_component = commerce_price_component_load($order->commerce_order_total
          ->value(), $tax_rate['price_component']);

        // Some taxes may not have been applied.
        if (isset($tax_component[0]['price']['amount'])) {
          $tax_sum += commerce_currency_amount_to_decimal($tax_component[0]['price']['amount'], $tax_component[0]['price']['currency_code']);
        }
      }
    }
  }
  $shipping = 0;
  if (module_exists('commerce_shipping')) {
    foreach ($order->commerce_line_items as $item) {
      if ($item->type
        ->value() == 'shipping') {
        $shipping += commerce_currency_amount_to_decimal($item->commerce_total->amount
          ->value(), $item->commerce_total->currency_code
          ->value());
      }
    }
  }

  // Build the transaction arguments.
  $order_id = $order->order_id
    ->value();
  $order_number = $order->order_number
    ->value() ? $order->order_number
    ->value() : $order_id;
  $order_currency_code = $order->commerce_order_total->currency_code
    ->value();
  $order_total = commerce_currency_amount_to_decimal($order->commerce_order_total->amount
    ->value(), $order_currency_code);
  $affiliation = variable_get('site_name', '');
  $order_data = array(
    'id' => $order_number,
    'affiliation' => $affiliation,
    'revenue' => $order_total,
    'tax' => $tax_sum,
    'shipping' => $shipping,
  );

  // Allow other modules to alter this order data.
  $context = array(
    'order' => $order,
    'event' => $event,
  );
  drupal_alter('commerce_google_tag_manager_order_data', $order_data, $context);
  return $order_data;
}