You are here

function commerce_email_prepare_table in Commerce Email 7.2

Returns a table header and rows for theming.

Parameters

$wrapper: A metadata-wrapped commerce_order entity

Return value

array Array suitable for use by theme('table' ...)

2 calls to commerce_email_prepare_table()
commerce_email_order_items in ./commerce_email.module
Returns a rendered email of the commerce order or an array of the table details
theme_commerce_email_order_items in ./commerce_email.module
Theme function for the commerce order.

File

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

Code

function commerce_email_prepare_table($wrapper) {
  $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' => $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;
    }
  }
  $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 (strpos($data['components'][$key]['name'], 'discount') !== FALSE) {
        $rows[] = array(
          'data' => array(
            ' ',
            array(
              'data' => $data['components'][$key]['price']['data']['discount_component_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;',
              ),
            ),
          ),
        );
      }
      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;',
        ),
      ),
    ),
  );
  return array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => array(
      'style' => array(
        'width: 50%; border: 1px solid #ddd;',
      ),
    ),
  );
}