You are here

function basic_cart_checkout_form_submit in Basic cart 7

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

Checkout form submit proccess. Sending the 2 mails.

File

./basic_cart.cart.inc, line 271
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();
  foreach ($cart as $nid => $node) {
    $order_details .= $node->basic_cart_quantity . ' x ' . $node->title . "\n";
  }
  $order_details .= "\n";

  // Pleaceholder replacement.
  $search = array(
    '%CUSTOMER_NAME',
    '%CUSTOMER_EMAIL',
    '%CUSTOMER_PHONE',
    '%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_address'],
    $form_state['values']['basic_cart_checkout_message'],
    $order_details,
  );

  // Admin mail.
  $message_html = variable_get('basic_cart_admin_message');
  $message_html = str_replace($search, $replace, $message_html);
  $params['admin_message'] = $message_html;
  $site_mail = variable_get('site_mail');

  // Sending mail.
  $message = drupal_mail('basic_cart', 'admin_mail', $site_mail, language_default(), $params);
  $mails_sent = 0;
  if ($message['result']) {
    $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');
  }
}