You are here

function commerce_email_order_items in Commerce Email 7

Same name and namespace in other branches
  1. 7.2 commerce_email.module \commerce_email_order_items()

Returns a rendered email of the commerce order or an array of the order details

Parameters

$order: The commerce order object

$theme: (optional) Defaults to FALSE Flag to return the contents of the order as a html table or an array

Return value

String containing the rendered order table or an array of the order

1 call to commerce_email_order_items()
commerce_email_order_email_send in ./commerce_email.rules.inc
Rules action: Send the users order as an HTML email

File

./commerce_email.module, line 422
Defines additional menu item and order html email functonality.

Code

function commerce_email_order_items($order, $theme = TRUE) {
  $wrapper = entity_metadata_wrapper('commerce_order', $order);
  $currency_code = $wrapper->commerce_order_total->currency_code
    ->value();
  $amount = number_format(commerce_currency_amount_to_decimal($wrapper->commerce_order_total->amount
    ->value(), $currency_code), 2);
  $header = array(
    array(
      'data' => t('Product'),
      'style' => array(
        'text-align: left;',
      ),
    ),
    array(
      'data' => t('Qty'),
      'style' => array(
        'text-align: left;',
      ),
    ),
    array(
      'data' => t('Price (@currency_code)', array(
        '@currency_code' => $currency_code,
      )),
      'style' => array(
        'text-align: left;',
      ),
    ),
  );
  $rows = array();
  foreach ($wrapper->commerce_line_items as $delta => $line_item_wrapper) {
    switch ($line_item_wrapper->type
      ->value()) {
      case 'product':

        // Special treatment for a product, since we want to get the title from
        // from the product entity instead of the line item.
        $title = htmlentities($line_item_wrapper->commerce_product->title
          ->value(), ENT_QUOTES, "UTF-8");
        $title .= commerce_email_order_item_attributes($line_item_wrapper->commerce_product->product_id
          ->value());
        $rows[] = array(
          'data' => array(
            array(
              'data' => $title,
              'style' => array(
                'text-align: left;',
              ),
            ),
            array(
              'data' => $line_item_wrapper->quantity
                ->value(),
              'style' => array(
                'text-align: left;',
              ),
            ),
            array(
              'data' => number_format(commerce_currency_amount_to_decimal($line_item_wrapper->commerce_unit_price->amount
                ->value(), $currency_code), 2),
              'style' => array(
                'text-align: left;',
              ),
            ),
          ),
        );
        break;
      default:

        // Use this for any other line item.
        $rows[] = array(
          'data' => array(
            array(
              'data' => htmlentities($line_item_wrapper->line_item_label
                ->value(), ENT_QUOTES, "UTF-8"),
              'style' => array(
                'text-align: left;',
              ),
            ),
            array(
              'data' => 1,
              'style' => array(
                'text-align: left;',
              ),
            ),
            array(
              'data' => number_format(commerce_currency_amount_to_decimal($line_item_wrapper->commerce_unit_price->amount
                ->value(), $currency_code), 2),
              'style' => array(
                'text-align: left;',
              ),
            ),
          ),
        );
        break;
    }
  }
  $data = $wrapper->commerce_order_total->data
    ->value();
  if (!empty($data['components'])) {
    foreach ($data['components'] as $key => &$component) {
      if ($data['components'][$key]['name'] == 'base_price') {
        $rows[] = array(
          'data' => array(
            ' ',
            array(
              'data' => t('Subtotal:'),
              'style' => array(
                'font-weight: bold; text-align: right;',
              ),
            ),
            array(
              'data' => number_format(commerce_currency_amount_to_decimal($data['components'][$key]['price']['amount'], $currency_code), 2),
              'style' => array(
                'font-weight: bold; text-align: left;',
              ),
            ),
          ),
        );
      }
      elseif (preg_match('/^tax\\|/', $data['components'][$key]['name'])) {
        $rows[] = array(
          'data' => array(
            ' ',
            array(
              'data' => $data['components'][$key]['price']['data']['tax_rate']['display_title'] . ':',
              'style' => array(
                'font-weight: bold; text-align: right;',
              ),
            ),
            array(
              'data' => number_format(commerce_currency_amount_to_decimal($data['components'][$key]['price']['amount'], $currency_code), 2),
              'style' => array(
                'font-weight: bold; text-align: left;',
              ),
            ),
          ),
        );
      }
    }
  }
  $rows[] = array(
    'data' => array(
      ' ',
      array(
        'data' => t('Total:'),
        'style' => array(
          'font-weight: bold; text-align: right;',
        ),
      ),
      array(
        'data' => $amount,
        'style' => array(
          'font-weight: bold; text-align: left;',
        ),
      ),
    ),
  );
  if ($theme) {
    return theme('table', array(
      'header' => $header,
      'rows' => $rows,
      'attributes' => array(
        'style' => array(
          'width: 50%; border: 1px solid #ddd;',
        ),
      ),
    ));
  }
  else {
    return array(
      'header' => $header,
      'rows' => $rows,
      'attributes' => array(
        'style' => array(
          'width: 50%; border: 1px solid #ddd;',
        ),
      ),
    );
  }
}