You are here

basic_cart_order.module in Basic cart 7.3

Same filename and directory in other branches
  1. 7.2 basic_cart_order/basic_cart_order.module

File

basic_cart_order/basic_cart_order.module
View source
<?php

/**
 * @file
 * Code for the Basic Cart Order feature.
 */
include_once 'basic_cart_order.features.inc';
module_load_include('inc', 'basic_cart', 'basic_cart.cart');

/**
 * Implements hook_node_view().
 */
function basic_cart_order_view($node) {
  $oid = $node->nid;

  // Getting the order products.
  $order_products = db_select('basic_cart_order_node')
    ->fields('basic_cart_order_node')
    ->condition('oid', $oid)
    ->execute()
    ->fetchAll();

  // Building the products array.
  if (is_array($order_products)) {
    $products = array();
    foreach ($order_products as $product) {
      $p = node_load($product->nid);
      $p->basic_cart_quantity = $product->quantity;

      // Price in a nicer form.
      $price = field_get_items('node', $p, 'price');
      $price = isset($price[0]['value']) ? check_plain($price[0]['value']) : '';
      $p->price = basic_cart_price_format($price);
      $products[] = $p;
    }
  }

  // Building the order variables.

  //$order = node_load($oid);
  $order = $node;

  // Name.
  $name = check_plain($order->title);

  // Email.
  $email = field_get_items('node', $order, 'field_email');
  $email = isset($email[0]['value']) ? check_plain($email[0]['value']) : '';

  // Phone.
  $phone = field_get_items('node', $order, 'field_phone');
  $phone = isset($phone[0]['value']) ? check_plain($phone[0]['value']) : '';

  // City.
  $city = field_get_items('node', $order, 'field_city');
  $city = isset($city[0]['value']) ? check_plain($city[0]['value']) : '';

  // Zip Code.
  $zipcode = field_get_items('node', $order, 'field_zipcode');
  $zipcode = isset($zipcode[0]['value']) ? check_plain($zipcode[0]['value']) : '';

  // Adsress.
  $address = field_get_items('node', $order, 'field_address');
  $address = isset($address[0]['value']) ? check_plain($address[0]['value']) : '';

  // Message.
  $message = field_get_items('node', $order, 'body');
  $message = isset($message[0]['value']) ? check_plain($message[0]['value']) : '';

  // Total price.
  $total_price = field_get_items('node', $order, 'field_total_price');
  $total_price = isset($total_price[0]['value']) ? check_plain($total_price[0]['value']) : '';
  $total_price = basic_cart_price_format($total_price);

  // VAT.
  $vat = NULL;
  $vat_is_enabled = (int) variable_get('basic_cart_vat_state');
  if (!empty($vat_is_enabled) && $vat_is_enabled) {
    $vat = field_get_items('node', $order, 'vat');
    $vat = isset($vat[0]['value']) ? check_plain($vat[0]['value']) : '';
    $vat = basic_cart_price_format($vat);
  }
  $pid = db_query("SELECT pid FROM {basic_cart_order_payment} WHERE oid = :oid", array(
    ':oid' => $oid,
  ))
    ->fetchAssoc();
  if (isset($pid['pid'])) {
    $pid = $pid['pid'];
  }
  else {
    drupal_set_message(t('Error: empty payment ID.'), 'error');
  }
  $payment = entity_load_single('payment', $pid);
  if (payment_access('view', $payment)) {
    $view = ' (' . l(t('View payment'), 'payment/' . $payment->pid) . ')';
  }
  if (payment_status_is_or_has_ancestor($payment
    ->getStatus()->status, PAYMENT_STATUS_PENDING)) {
    $status = 'PENDING' . $view;
  }
  elseif (payment_status_is_or_has_ancestor($payment
    ->getStatus()->status, PAYMENT_STATUS_SUCCESS)) {
    $status = 'SUCCESS' . $view;
  }
  elseif (payment_status_is_or_has_ancestor($payment
    ->getStatus()->status, PAYMENT_STATUS_FAILED)) {
    $status = 'FAILED' . $view;
  }

  // Building the payment information array.
  $payment_arr = array(
    'status' => $status,
    'method' => $payment->method->title_specific,
  );

  // Hide field data.
  unset($node->content['field_email']);
  unset($node->content['field_phone']);
  unset($node->content['field_city']);
  unset($node->content['field_zipcode']);
  unset($node->content['field_address']);
  unset($node->content['field_total_price']);

  // Page title.
  $title = t('Order ID: @oid', array(
    '@oid' => $oid,
  ));
  drupal_set_title($title);
  return theme('basic_cart_order_details', array(
    'name' => $name,
    'email' => $email,
    'phone' => $phone,
    'city' => $city,
    'zipcode' => $zipcode,
    'address' => $address,
    'message' => $message,
    'products' => $products,
    'total_price' => $total_price,
    'vat' => $vat,
    'timestamp' => $node->created,
    'payment' => $payment_arr,
  ));
}

/**
 * Implements hook_menu().
 */
function basic_cart_order_menu() {
  $items = array();
  $items['admin/structure/orders/%node'] = array(
    'title' => 'Basic Cart Orders',
    'page callback' => 'basic_cart_order_view',
    'page arguments' => array(
      3,
    ),
    'access arguments' => array(
      'view basic cart orders',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/config/basic_cart/settings/checkout'] = array(
    'title' => 'Checkout',
    'description' => 'Basic cart checkout settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'basic_cart_admin_checkout',
    ),
    'access arguments' => array(
      'administer basic cart',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'basic_cart_order.admin.inc',
    'weight' => 10,
  );
  $items['checkout'] = array(
    'title' => 'Checkout',
    'description' => 'Checkout.',
    'page callback' => 'basic_cart_order_checkout',
    'access arguments' => array(
      'use basic cart',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['checkout/thank-you'] = array(
    'title' => 'Thank you',
    'description' => 'Checkout thank you page.',
    'page callback' => 'basic_cart_order_checkout_thank_you',
    'access arguments' => array(
      'use basic cart',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function basic_cart_order_theme() {
  return array(
    'basic_cart_order_details' => array(
      'template' => 'basic_cart_order_details',
      'variables' => array(
        'name' => NULL,
        'email' => NULL,
        'phone' => NULL,
        'city' => NULL,
        'zipcode' => NULL,
        'address' => NULL,
        'message' => NULL,
        'products' => NULL,
        'total_price' => NULL,
        'vat' => NULL,
        'timestamp' => NULL,
        'payment' => NULL,
      ),
    ),
  );
}

/**
 * Checkout.
 */

/**
 * Checkout form implementation.
 */
function basic_cart_order_checkout() {
  $shopping_cart = basic_cart_get_cart();

  // Price.
  $price = basic_cart_get_total_price();
  $total = basic_cart_price_format($price->total);
  $options = array(
    'cart' => $shopping_cart,
    'price' => $total,
  );

  // Checking the VAT.
  $vat_is_enabled = (int) variable_get('basic_cart_vat_state');
  if (!empty($vat_is_enabled) && $vat_is_enabled) {
    $options['vat'] = basic_cart_price_format($price->vat);
  }

  // The flat cart (just the listing part).
  $cart = theme('basic_cart_cart_flat', $options);

  // If the cart is empty, we don't want to show the checkout form.
  if (empty($shopping_cart)) {
    return $cart;
  }

  // Using the core node/add form for more flexibility.
  module_load_include('inc', 'node', 'node.pages');
  $form = node_add('order');

  // Removing the total price field.
  unset($form['field_total_price']);
  unset($form['vat']);
  $form['actions']['submit']['#value'] = t('Submit order');
  $form = drupal_render($form);
  return $cart . $form;
}

/**
 * Sending emails.
 */
function basic_cart_order_send_notifications($order) {

  // Admin message.
  $message_html = t(variable_get('basic_cart_admin_message'));

  // 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 = variable_get('site_mail');
  $admin_emails = variable_get('basic_cart_admin_emails');
  if (empty($admin_emails)) {

    // Sending mail to admin.
    $message = drupal_mail('basic_cart_order', 'admin_mail', $site_mail, language_default(), $params);
    $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 = drupal_mail('basic_cart_order', 'admin_mail', $admin_email, language_default(), $params);

        // 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 = variable_get('basic_cart_send_user_message');
  if ($send_user_mail) {
    $message_html = t(variable_get('basic_cart_user_message'));

    // 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 = field_get_items('node', $order, 'field_email');
    $email = $email[0]['value'];
    $message = drupal_mail('basic_cart_order', 'user_mail', $email, language_default(), $params);
    if ($message['result']) {
      $mails_sent++;
    }
  }
  return $mails_sent;
}

/**
 * Implements hook_mail().
 */
function basic_cart_order_mail($key, &$message, $params) {
  switch ($key) {
    case 'admin_mail':
      $message['subject'] = t(check_plain(variable_get('basic_cart_admin_subject')));
      $message['body'][] = filter_xss($params['admin_message']);
      break;
    case 'user_mail':
      $message['subject'] = t(check_plain(variable_get('basic_cart_user_subject')));
      $message['body'][] = filter_xss($params['user_message']);
      break;
  }
}

/**
 * Callback for thank you page.
 */
function basic_cart_order_checkout_thank_you() {
  $title = t(variable_get('basic_cart_thank_you_title'));
  drupal_set_title($title);
  $message = t(variable_get('basic_cart_thank_you_message'));
  return filter_xss($message);
}

/**
 * Implements hook_node_insert().
 * 
 * Adding order details and sending emails.
 */
function basic_cart_order_node_insert($node) {

  // The node is of type order.
  if ($node->type == 'order') {

    // Getting the cart.
    $cart = basic_cart_get_cart();
    if (!is_array($cart)) {
      return NULL;
    }

    // Total price.
    $price = basic_cart_get_total_price();
    $node->field_total_price[$node->language][0]['value'] = $price->total;
    $node->field_total_price[LANGUAGE_NONE][0]['value'] = $price->total;

    // VAT.
    $vat_is_enabled = (int) variable_get('basic_cart_vat_state');
    if (!empty($vat_is_enabled) && $vat_is_enabled) {
      $node->vat[$node->language][0]['value'] = $price->vat;
      $node->vat[LANGUAGE_NONE][0]['value'] = $price->vat;
    }
    field_attach_update('node', $node);
    $oid = $node->nid;
    foreach ($cart as $n) {
      $record = array(
        'oid' => $oid,
        'nid' => $n->nid,
        'quantity' => $n->basic_cart_quantity,
      );
      drupal_write_record('basic_cart_order_node', $record);
    }

    // If the basic_cart_payment module is enabled
    // we need to register the order ID to the payment ID.
    if (module_exists('basic_cart_payment')) {
      $pid = basic_cart_payment_pid();
      if (!empty($pid)) {
        db_insert('basic_cart_order_payment')
          ->fields(array(
          'oid' => $oid,
          'pid' => $pid,
        ))
          ->execute();
      }
    }

    // Seding email notifications.
    $emails = basic_cart_order_send_notifications($node);
    if ($emails >= 1) {
      basic_cart_empty_cart();

      // We will switch the view, depending on if admin or not.

      //drupal_goto('checkout/thank-you');
    }
    else {
      drupal_set_message(t('There was a problem in submitting your order. Please try again later.'), 'error');
    }
  }
}

/**
 * Callback function for formatting the order tokens.
 * 
 * @param array $tokens
 *   The tockens list.
 * 
 * @return array $tokens.
 *   The formatted list of tokens
 */
function basic_cart_order_tokens_format(&$tokens) {
  if (is_array($tokens) && count($tokens) > 0) {
    foreach ($tokens as $token => $value) {
      switch ($token) {
        case '[node:field_total_price]':
          $tokens[$token] = basic_cart_price_format($value);
          break;
      }
    }
  }
  return $tokens;
}

/**
 * Implements hook_token_info().
 */
function basic_cart_order_token_info() {
  $info = array();

  // Add any new tokens.
  $info['tokens']['basic_cart_order']['products'] = array(
    'name' => t('Product listing'),
    'description' => t('Listing of ordered products.'),
  );

  // Return them.
  return $info;
}

/**
 * Implements hook_tokens().
 */
function basic_cart_order_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  // The first thing that we're going to check for is the type of token - node,
  // user etc...
  if ($type == 'basic_cart_order') {

    // Loop through each of the available tokens.
    foreach ($tokens as $name => $original) {

      // Find the desired token by name
      switch ($name) {
        case 'products':
          $new = '';
          $cart = basic_cart_get_cart();

          // Building the order details.
          $i = 0;
          $products = '';
          foreach ($cart as $nid => $node) {
            $unit_price = basic_cart_price_format($node->basic_cart_unit_price);
            $new .= ++$i . '. ' . $node->title . "\t" . $node->basic_cart_quantity . ' x ' . $unit_price . "\n";
          }

          // Add the new value into the replacements array.
          $replacements[$original] = $new;
          break;
      }
    }
  }

  // Return the replacements.
  return $replacements;
}

Functions

Namesort descending Description
basic_cart_order_checkout Checkout form implementation.
basic_cart_order_checkout_thank_you Callback for thank you page.
basic_cart_order_mail Implements hook_mail().
basic_cart_order_menu Implements hook_menu().
basic_cart_order_node_insert Implements hook_node_insert().
basic_cart_order_send_notifications Sending emails.
basic_cart_order_theme Implements hook_theme().
basic_cart_order_tokens Implements hook_tokens().
basic_cart_order_tokens_format Callback function for formatting the order tokens.
basic_cart_order_token_info Implements hook_token_info().
basic_cart_order_view Implements hook_node_view().