You are here

function _uc_stock_send_mail in Ubercart 8.4

Same name and namespace in other branches
  1. 5 uc_stock/uc_stock.module \_uc_stock_send_mail()
  2. 6.2 uc_stock/uc_stock.module \_uc_stock_send_mail()
  3. 7.3 uc_stock/uc_stock.module \_uc_stock_send_mail()

Emails administrator regarding any stock level thresholds hit.

Parameters

\Drupal\uc_order\OrderInterface $order: The order object that tripped the threshold limit.

\Drupal\node\NodeInterface $node: The node object that is associated with the SKU/model.

$stock: The stock level object that contains the stock level and SKU.

1 call to _uc_stock_send_mail()
uc_stock_adjust_product_stock in uc_stock/uc_stock.module
Decrement a product's stock.

File

uc_stock/uc_stock.module, line 151
Allow ubercart products to have stock levels associated with their SKU.

Code

function _uc_stock_send_mail(OrderInterface $order, NodeInterface $node, $stock) {
  $token_service = \Drupal::token();
  $account = $order
    ->getOwner();
  $token_filters = [
    'uc_order' => $order,
    'user' => $account,
    'uc_stock' => $stock,
    'node' => $node,
  ];
  $to = \Drupal::config('uc_stock.settings')
    ->get('recipients');
  $to = $token_service
    ->replace($to, $token_filters);
  $to = explode(',', $to);
  $from = uc_store_email_from();
  $mail_config = \Drupal::config('uc_stock.mail');
  $subject = $mail_config
    ->get('threshold_notification.subject');
  $subject = $token_service
    ->replace($subject, $token_filters);
  $body = $mail_config
    ->get('threshold_notification.body');
  $body = $token_service
    ->replace($body, $token_filters);

  // Send to each recipient.
  foreach ($to as $email) {
    $sent = \Drupal::service('plugin.manager.mail')
      ->mail('uc_stock', 'threshold', $email, uc_store_mail_recipient_langcode($email), [
      'body' => $body,
      'subject' => $subject,
      'order' => $order,
      'stock' => $stock,
    ], $from);
    if (!$sent['result']) {
      \Drupal::logger('uc_stock')
        ->error('Attempt to e-mail @email concerning stock level on sku @sku failed.', [
        '@email' => $email,
        '@sku' => $stock->sku,
      ]);
    }
  }
}