You are here

function basic_cart_order_send_notifications in Basic cart 8.4

Same name and namespace in other branches
  1. 8.6 basic_cart.module \basic_cart_order_send_notifications()
  2. 8 basic_cart.module \basic_cart_order_send_notifications()
  3. 8.0 basic_cart.module \basic_cart_order_send_notifications()
  4. 8.2 basic_cart.module \basic_cart_order_send_notifications()
  5. 8.3 basic_cart.module \basic_cart_order_send_notifications()
  6. 8.5 basic_cart.module \basic_cart_order_send_notifications()
  7. 7.3 basic_cart_order/basic_cart_order.module \basic_cart_order_send_notifications()

Sending emails.

1 call to basic_cart_order_send_notifications()
basic_cart_entity_insert in ./basic_cart.module
Implements hook_entity_insert().

File

./basic_cart.module, line 160
Basic cart module file.

Code

function basic_cart_order_send_notifications($order) {
  $mailManager = \Drupal::service('plugin.manager.mail');

  // Admin message.
  $utility = new Utility();
  $token = \Drupal::token();
  $config = $utility
    ->checkoutSettings();
  $message_html = $config
    ->get('admin')['body'];

  // Token data.
  $data = array(
    'node' => $order,
  );
  $message_html = $token
    ->replace($message_html, $data, array(
    'callback' => 'basic_cart_order_tokens_format',
  ));

  // Admin mail.
  $params['admin_message'] = $message_html;
  $site_mail = \Drupal::config('system.site')
    ->get('mail');
  $send = TRUE;
  $admin_emails = $config
    ->get('admin_emails');
  $langcode = \Drupal::currentUser()
    ->getPreferredLangcode();
  if (empty($admin_emails)) {

    // Sending mail to admin.
    $message = $mailManager
      ->mail('basic_cart', 'admin_mail', $site_mail, $langcode, $params, NULL, $send);
    $mails_sent = 0;
    if ($message['result']) {
      $mails_sent++;
    }
  }
  else {
    $admin_emails = explode("\n", $admin_emails);
    if (is_array($admin_emails) && !empty($admin_emails)) {
      $ok = FALSE;
      foreach ($admin_emails as $admin_email) {

        // Sending mail to each admin.
        $message = $mailManager
          ->mail('basic_cart', 'admin_mail', $admin_email, $langcode, $params, NULL, $send);

        // Verifing that the mail was sent for at least one email address.
        if ($message['result']) {
          $ok = TRUE;
        }
      }
      $mails_sent = 0;
      if ($ok) {
        $mails_sent++;
      }
    }
  }

  // User email.
  $send_user_mail = $config
    ->get('send_emailto_user');
  if ($send_user_mail) {
    $message_html = $config
      ->get('user')['body'];

    // Token data.
    $data = array(
      'node' => $order,
    );
    $message_html = $token
      ->replace($message_html, $data, array(
      'callback' => 'basic_cart_order_tokens_format',
    ));
    $params['user_message'] = $message_html;

    // Sending mail.
    $email = $order
      ->getTranslation($langcode)
      ->get('basic_cart_email')
      ->getValue();
    $email = $email[0]['value'];
    $message = $mailManager
      ->mail('basic_cart', 'user_mail', $email, $langcode, $params, NULL, $send);
    if ($message['result']) {
      $mails_sent++;
    }
  }
  return $mails_sent;
}