You are here

function commerce_cart_expiration_expire_ajax in Commerce Cart Expiration 7

Ajax callback for commerce_cart_expiration/expire/%commerce_order.

Triggers order deletion instantly when javascript recognizes its expiration.

1 string reference to 'commerce_cart_expiration_expire_ajax'
commerce_cart_expiration_menu in ./commerce_cart_expiration.module
Implements hook_menu().

File

./commerce_cart_expiration.module, line 273
Provides a time-based cart expiration feature.

Code

function commerce_cart_expiration_expire_ajax($order) {
  global $user;

  // Return status to calling ajax function.
  // 0: Order was not deleted as it was processed in the meantime.
  // 1: Order deleted; redirect to the explanation page.
  $status = 0;

  // Since the user may have processed the order in another browser tab we need
  // to check its status again.
  $statuses = commerce_order_statuses(array(
    'cart' => TRUE,
  ));
  if (isset($statuses[$order->status])) {

    // Replace explanation page tokens here because this is the last point
    // where we can use the order.
    $text = variable_get('commerce_cart_expiration_explanation_page', '<p>' . t('Sorry, you took too much time in the checkout process.') . '</p><p>' . t('<a href="[site:url]">Return to the front page.</a>') . '</p>');
    if (is_array($text)) {
      $text = check_markup($text['value'], $text['format']);
    }
    $text = token_replace($text, array(
      'commerce-order' => $order,
      'site' => NULL,
      'user' => $user,
    ));

    // Store this text in user session:
    $_SESSION['commerce_cart_expiration_text'] = $text;

    // Invoke a Rules event for deleting an expired cart order.
    rules_invoke_all('commerce_cart_expiration_delete_order', $order);

    // Delete the order.
    commerce_order_delete($order->order_id);
    $status = 1;
  }
  drupal_json_output(array(
    'status' => $status,
  ));
  exit;
}