You are here

function commerce_email_order_email_send in Commerce Email 7

Rules action: Send the users order as an HTML email

1 string reference to 'commerce_email_order_email_send'
commerce_email_rules_action_info in ./commerce_email.rules.inc
Implements hook_rules_action_info().

File

./commerce_email.rules.inc, line 48
Defines rules for order email functionality.

Code

function commerce_email_order_email_send($order) {
  global $language, $base_url;
  $site_email = variable_get('site_mail', '');
  $commerce_email_template = variable_get('commerce_email_order_template_' . $language->language, 0);
  $site = array(
    'site_name' => variable_get('site_name', "Commerce Email (default)"),
    'site_url' => $base_url,
  );

  // Load email content
  list($subject, $content) = commerce_email_load('order');
  $message = array(
    'id' => 'commerce_email_order',
    'module' => 'commerce_email',
    'key' => 'order',
    'to' => $order->mail,
    'from' => $site_email,
    'subject' => token_replace($subject, array(
      'commerce-order' => $order,
    )),
    'body' => array(
      '',
    ),
    'headers' => array(
      'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
      'From' => $site_email,
      'Sender' => $site_email,
      'Return-Path' => $site_email,
    ),
  );

  // Order and Customer details
  $customer_profile = commerce_email_customer_profile($order);
  commerce_email_customer_name($order, $customer_profile);
  if ($commerce_email_template == 1) {
    $order->order_items = commerce_email_order_items($order, FALSE);
    $message['body'] = theme('commerce_order_email', array(
      'site' => $site,
      'order' => $order,
      'customer_profile' => $customer_profile,
      'language' => $language->language,
    ));
  }
  else {
    $order->order_items = commerce_email_order_items($order);
    $message['body'] = token_replace($content, array(
      'commerce-order' => $order,
      'commerce-email' => $order,
      'commerce-customer-profile' => $customer_profile,
    ), array(
      'sanitize' => FALSE,
    ));
  }

  //Send user order email
  commerce_email_mailsystem_send('order', $message);

  // Send admin order email
  $admin_email = variable_get('admin_email', array());
  if (!empty($admin_email['enabled'])) {
    $commerce_email_template = variable_get('commerce_email_admin_order_template_' . $language->language, 0);

    // Load email content
    list($subject, $content) = commerce_email_load('admin_order');
    $recipient = empty($admin_email['email_address']) ? $site_email : $admin_email['email_address'];
    $message = array(
      'id' => 'commerce_email_admin_order',
      'module' => 'commerce_email',
      'key' => 'admin_order',
      'to' => $recipient,
      'from' => $site_email,
      'subject' => token_replace($subject, array(
        'commerce-order' => $order,
      )),
      'body' => array(
        '',
      ),
      'headers' => array(
        'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
        'From' => $site_email,
        'Sender' => $site_email,
        'Return-Path' => $site_email,
      ),
    );
    if ($commerce_email_template == 1) {
      $order->order_items = commerce_email_order_items($order, FALSE);
      $message['body'] = theme('commerce_admin_order_email', array(
        'site' => $site,
        'order' => $order,
        'customer_profile' => $customer_profile,
        'language' => $language->language,
      ));
    }
    else {
      $order->order_items = commerce_email_order_items($order);
      $message['body'] = token_replace($content, array(
        'commerce-order' => $order,
        'commerce-email' => $order,
        'commerce-customer-profile' => $customer_profile,
      ), array(
        'sanitize' => FALSE,
      ));
    }

    // Send admin a copy of the user order email
    commerce_email_mailsystem_send('admin_order', $message);
  }
}