You are here

function commerce_registration_defer_form in Commerce Registration 7.2

Callback to get the registration deferral form.

1 string reference to 'commerce_registration_defer_form'
commerce_registration_defer_menu in modules/commerce_registration_defer/commerce_registration_defer.module
Implements hook_menu().

File

modules/commerce_registration_defer/commerce_registration_defer.module, line 54
Module file for commerce_registration_defer.

Code

function commerce_registration_defer_form($form, &$form_state) {
  $original_registration = $form_state['build_info']['args'][0];
  $order = $form_state['build_info']['args'][1];
  $deferred_product = commerce_product_load($original_registration->entity_id);
  $original_line_item = FALSE;

  // Find the line item associated to the provided registration and order.
  if (!empty($order->data['register_entities'])) {
    foreach ($order->data['register_entities'] as $key => $line_item_registrations) {
      $key_parts = explode('prod-', $key);
      if (count($key_parts) === 2) {
        $line_item_id = $key_parts[0];
        foreach ($line_item_registrations as $checkout_reg) {
          if ($checkout_reg->registration_id && $checkout_reg->registration_id == $original_registration->registration_id) {
            $original_line_item = commerce_line_item_load($line_item_id);
            break 2;
          }
        }
      }
    }
  }
  if ($original_line_item) {

    // Load the info object for the line item type associated with the product
    // we wish to defer. Assume we'll be using the same line item type for
    // the new product.
    $line_item_type = commerce_line_item_type_load($original_line_item->type);

    // Store the line item info object in the form array.
    $form['actions']['line_item_type'] = array(
      '#type' => 'value',
      '#value' => $line_item_type,
    );

    // Store the line item for the deferred product in the form array.
    $form['actions']['original_line_item'] = array(
      '#type' => 'value',
      '#value' => $original_line_item,
    );

    // If this type specifies a valid add form callback function...
    if ($callback = commerce_line_item_type_callback($line_item_type, 'add_form')) {

      // Load in the appropriate form elements to the actions array.
      $form['actions'] += $callback($form, $form_state);
    }
  }

  // Submit button.
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Defer registration'),
  );

  // Cancel link.
  $order_uri = commerce_order_ui_order_uri($order);
  $form['cancel'] = array(
    '#markup' => l(t('Cancel'), $order_uri['path'] . '/edit'),
  );

  // Add the confirmation message to the form so that it can be overridden.
  $form['confirmation'] = array(
    '#type' => 'value',
    '#value' => t('The registration has been deferred.'),
  );
  return $form;
}