You are here

function basic_cart_checkout_form_submit in Basic cart 7.2

Same name and namespace in other branches
  1. 7 basic_cart.cart.inc \basic_cart_checkout_form_submit()

Checkout form submit proccess. Register order and send emails.

File

./basic_cart.cart.inc, line 404
Basic cart shopping cart implementation functions.

Code

function basic_cart_checkout_form_submit($form, &$form_state) {

  // %ORDER_DETAILS% placeholder.
  $order_details = '';
  $cart = basic_cart_get_cart();
  $price = basic_cart_get_total_price();
  $total_price = basic_cart_price_format($price->total);
  $vat_is_enabled = (int) variable_get('basic_cart_vat_state');
  if (!empty($vat_is_enabled) && $vat_is_enabled) {
    $vat = basic_cart_price_format($price->vat);
  }

  // Registering the new order to the database.
  if (module_exists('basic_cart_order')) {
    $name = $form_state['values']['basic_cart_checkout_name'];
    $email = $form_state['values']['basic_cart_checkout_email'];
    $order_details_ = array(
      'phone' => $form_state['values']['basic_cart_checkout_phone'],
      'city' => $form_state['values']['basic_cart_checkout_city'],
      'zipcode' => $form_state['values']['basic_cart_checkout_zipcode'],
      'address' => $form_state['values']['basic_cart_checkout_address'],
      'message' => $form_state['values']['basic_cart_checkout_message'],
    );
    $order = basic_cart_order_register_order($name, $email, $order_details_);

    // Order successfully placed. Custom hook for other modules.
    foreach (module_implements('basic_cart_order') as $module) {
      $function = $module . '_basic_cart_order';

      // Will call all modules implementing hook_basic_cart_order() with the order node as argument.
      $function($order);
    }
  }

  // Building the order details.
  $i = 0;
  foreach ($cart as $nid => $node) {
    $unit_price = basic_cart_price_format($node->basic_cart_unit_price);
    $order_details .= ++$i . '. ' . $node->title . "\t" . $node->basic_cart_quantity . ' x ' . $unit_price . "\n";
  }
  $order_details .= "\n";
  $order_details .= "Total price: " . $total_price;
  $vat_is_enabled = (int) variable_get('basic_cart_vat_state');
  if ($vat_is_enabled) {
    $order_details .= "\n";
    $order_details .= "Total VAT: " . $vat;
  }

  // Pleaceholder replacement.
  $search = array(
    '%CUSTOMER_NAME',
    '%CUSTOMER_EMAIL',
    '%CUSTOMER_PHONE',
    '%CUSTOMER_CITY',
    '%CUSTOMER_ZIPCODE',
    '%CUSTOMER_ADDRESS',
    '%CUSTOMER_MESSAGE',
    '%ORDER_DETAILS',
  );
  $replace = array(
    $form_state['values']['basic_cart_checkout_name'],
    $form_state['values']['basic_cart_checkout_email'],
    $form_state['values']['basic_cart_checkout_phone'],
    $form_state['values']['basic_cart_checkout_city'],
    $form_state['values']['basic_cart_checkout_zipcode'],
    $form_state['values']['basic_cart_checkout_address'],
    $form_state['values']['basic_cart_checkout_message'],
    $order_details,
  );

  // Admin message.
  $message_html = variable_get('basic_cart_admin_message');
  $message_html = str_replace($search, $replace, $message_html);

  // 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', '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', '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 = variable_get('basic_cart_user_message');
    $message_html = str_replace($search, $replace, $message_html);
    $params['user_message'] = $message_html;

    // Sending mail.
    $message = drupal_mail('basic_cart', 'user_mail', $form_state['values']['basic_cart_checkout_email'], language_default(), $params);
    if ($message['result']) {
      $mails_sent++;
    }
  }
  if ($mails_sent >= 1) {
    basic_cart_empty_cart();
    drupal_goto('checkout/thank-you');
  }
  else {
    drupal_set_message(t('There was a problem in submitting your order. Please try again later.'), 'error');
  }
}