You are here

function uc_discounts_uc_order_pane_callback in Ubercart Discounts (Alternative) 7.2

Callback for hook_uc_order_pane().

1 string reference to 'uc_discounts_uc_order_pane_callback'
uc_discounts_uc_order_pane in uc_discounts/uc_discounts.module
Implements hook_uc_order_pane().

File

uc_discounts/uc_discounts.module, line 385

Code

function uc_discounts_uc_order_pane_callback($op, $order, &$form = NULL, &$form_state = NULL) {
  switch ($op) {
    case 'edit-form':
      $form['uc_discounts']['uc_discounts_codes'] = array(
        '#type' => 'textarea',
        '#rows' => 3,
      );
      $form['uc_discounts']['uc_discounts_button'] = array(
        '#type' => 'submit',
        '#value' => t('Apply discounts'),
      );
      return $form;
    case 'edit-process':
      $order_org = clone $order;
      if (isset($form_state['values']['uc_discounts_codes'])) {
        $order->uc_discounts_codes = uc_discounts_codes_to_array($form_state['values']['uc_discounts_codes']);
      }
      $results = uc_discounts_get_discounts_for_order($order, TRUE);

      // Make sure the recorded discount codes for the order were only the ones
      // that were actually used for a discount.
      $applied_codes = array();
      foreach ($results['discounts'] as $discount) {
        if (isset($discount->code) && !empty($discount->code)) {
          $applied_codes[] = $discount->code;
        }
      }
      $order->uc_discounts_codes = $applied_codes;
      foreach ($results['messages']['errors'] as $error) {
        drupal_set_message($error, "error");
      }
      foreach ($results['messages']['warnings'] as $warning) {
        drupal_set_message(t('Warning: @warning', array(
          '@warning' => $warning,
        )), 'error');
      }

      // Add discount line items to order.
      uc_discounts_add_line_items_to_order($order, $results['discounts']);
      $changes = array();
      if ($order->uc_discounts_codes != $order_org->uc_discounts_codes) {
        $changes['uc_discounts_codes'] = $order->uc_discounts_codes;
      }
      if ($order->uc_discounts_line_items != $order_org->uc_discounts_line_items) {
        $changes['uc_discounts_line_items_need_updating'] = TRUE;
        $changes['uc_discounts_line_items'] = $order->uc_discounts_line_items;
      }
      return $changes;
  }
}