mocean_sms_abandoned_cart-8.x-1.1/mocean_sms_abandoned_cart.module

mocean_sms_abandoned_cart.module
<?php

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\mocean_sms_abandoned_cart\Utility;

/**
 * Implements hook_help().
 */
function mocean_sms_abandoned_cart_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.mocean_sms_abandoned_cart':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('MoceanSMS Abandoned Cart module sends alert SMS to your customers when they have an abandoned cart from Drupal Commerce
						by using <a href=":mocean_link">Mocean</a>SMS service.', [':mocean_link' => 'https://moceanapi.com/']) . '</p>';
      $output .= '<p>' . t('This module sends SMS to notify your customers regarding their incomplete orders by using 
						<a href=":der_link">Profile</a> which comes along with Drupal Commerce. 
						Users are required to configure the machine name for telephone field in Configuration.', 
						[':der_link' => 'https://www.drupal.org/project/profile']) . '</p>';
	  $output .= '<p>' . t('We also recommend <a href=":tv_link">Telephone Validation</a> to ensure phone numbers are in correct 
						format.', [':tv_link' => 'https://www.drupal.org/project/telephone_validation']) . '</p>';
      return $output;
  }
}

/**
 * Implements hook_cron().
 */
function mocean_sms_abandoned_cart_cron() {
  $sms_order_notification_config = \Drupal::config('mocean_sms_abandoned_cart.config');
  $enable = $sms_order_notification_config->get('enable');
  $send_timeout = $sms_order_notification_config->get('send_timeout') * 60;
  $history_limit = $sms_order_notification_config->get('history_limit') * 60;
  $batch_limit = $sms_order_notification_config->get('batch_limit');
  $message = $sms_order_notification_config->get('content');
  
  if (!$enable) 
    return;

  $currentTime = \Drupal::time()->getRequestTime();  
  $success = 0;
  $row = 0;
  $order_list = (new Utility)->getOrderList();
  
  try {
    foreach($order_list as $order) {   
      if ($row >= $batch_limit) 
        return;

      $wasSent = \Drupal::database()->select('mocean_sms_abandoned_cart', 'n')
          ->fields('n')
          ->condition('order_id', $order['order_id'],'=')
          ->execute()
          ->fetchAssoc();

      if($wasSent) {
        continue;
      }
      else if($order['created'] >= $currentTime -  $send_timeout) {
        continue;
      }
      else if($order['created'] <= $currentTime - $history_limit) {
        continue;
      }
      
      $placeholder = ['{store_name}', '{store_email}'];
      $replace = [$order['store_name'], $order['store_email']];
      $message = str_replace($placeholder, $replace, $message);
    
      $jsonResponse = (new Utility)->smsAbandonedCartSendMessage($order['recipient'], $message);
     
      if (count($jsonResponse) == count($jsonResponse, COUNT_RECURSIVE)) {
	    $status = $jsonResponse['status'];
	  }
      else {
	    $status = $jsonResponse['messages'][0]['status'];
	  }
	  
	  if($status == 0) {
        \Drupal::database()->insert('mocean_sms_abandoned_cart')
  	      ->fields([
	        'order_id'  => $order['order_id'],
		    'sent_time' => \Drupal::time()->getRequestTime(),
	      ])
          ->execute();
	    $success++;
	  }
      else if($status == 1) {
        throw new Exception('Aborted operation to send alert SMS for Commerce abandoned carts, authorization failed or low on credit.');
      }
      $row++;
    }
  }
  catch(Exception $e) {
    \Drupal::logger('mocean_sms_abandoned_cart')->error($e->getMessage());
  }

  if ($success != 0) {
    \Drupal::logger('mocean_sms_abandoned_cart')->notice('Alert SMS for Commerce abandoned carts successfully sent '.$success.' out of '.$row.'.');
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc