3

I have to build a module to add custom charges along with the order, e.g., gift wrapping charges.

I am using Drupal 7.

1
  • Questions about tutorials are not so much helpful, as the answers just contain a link to a tutorial. It is much preferable a question where the users can show code.
    – apaderno
    Mar 15, 2012 at 13:03

1 Answer 1

6

I've done exactly this before; I'll dump the code I used. Hopefully, it'll give you an idea.

/**
 * Implements hook_menu().
 */
function uc_giftwrap_menu() {
  $items['admin/store/settings/giftwrap'] = array(
    'title' => 'Gift Wrapping',
    'access arguments' => array('administer gift wrap'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('uc_giftwrap_admin_form')
  );

  return $items;
}

/**
 * Implements hook_permission().
 */
function uc_giftwrap_permission() {
  return array(
    'administer gift wrap' => array(
      'title' => 'Administer Gift Wrap Settings'
    )
  );
}

/**
 * Form builder for the gift wrap settings.
 */
function uc_giftwrap_admin_form($form, &$form_state) {
  $form['uc_giftwrap_cost'] = array(
    '#type' => 'textfield',
    '#title' => t('Gift Wrap Cost'),
    '#description' => t('Total cost of gift wrapping for an order.'),
    '#default_value' => round(variable_get('uc_giftwrap_cost', '0.00'), 2),
    '#field_prefix' => '£',
    '#size' => 10
  );

  return system_settings_form($form);
}

/**
 * Implements hook_uc_checkout_pane().
 */
function uc_giftwrap_uc_checkout_pane() {
  $panes['gift_wrap'] = array(
    'callback' => 'uc_giftwrap_uc_checkout_pane_callback',
    'title' => 'Gift Wrapping',
    'desc' => 'Provide gift wrapping options for the checkout.',
    'weight' => 5
  );

  return $panes;
}

/**
 * Callback for the checkout pane.
 */
function uc_giftwrap_uc_checkout_pane_callback($op, $order, $form = NULL, &$form_state = NULL) {
  if ($op == 'view') {
    $description = t('Please check this box if you wish your order to be gift wrapped.');

    $contents['gift_wrap'] = array(
      '#type' => 'checkbox',
      '#title' => t('Add Gift Wrapping') . ' (' . uc_currency_format(variable_get('uc_giftwrap_cost', 0)) . ')',
      '#default_value' => isset($_SESSION['uc_giftwrap_checkout']) ? 1 : 0,
      '#ajax' => array(
        'callback' => 'uc_giftwrap_update_line_items',
        'effect' => 'slide'
      )
    );

    return array('description' => $description, 'contents' => $contents);
  }
  else if ($op == 'process') {
    if ($form_state['values']['panes']['gift_wrap']['gift_wrap'] == 1) {
      if (!isset($_SESSION['uc_giftwrap_checkout'])) {
        uc_order_line_item_add($order->order_id, 'gift_wrap', 'Gift Wrapping', variable_get('uc_giftwrap_cost', 0));
        $id = db_query('SELECT MAX(line_item_id) FROM {uc_order_line_items}')->fetchField();
        $_SESSION['uc_giftwrap_checkout'] = $id;
      }
    }
    else {
      if (isset($_SESSION['uc_giftwrap_checkout'])) {
        uc_order_delete_line_item($_SESSION['uc_giftwrap_checkout']);
        unset($_SESSION['uc_giftwrap_checkout']);
      }
    }
  }
}

/**
 * AJAX callback for the checkout form (allows instant updating of total when gift wrap option is chosen).
 */
function uc_giftwrap_update_line_items($form, $form_state) {
  $commands = array();
  if (isset($form['panes']['payment']['line_items'])) {
    $commands[] = ajax_command_replace('#line-items-div', drupal_render($form['panes']['payment']['line_items']));
    $commands[] = ajax_command_prepend('#line-items-div', theme('status_messages'));
  }

  return array('#type' => 'ajax', '#commands' => $commands);
}

/**
 * Implements hook_uc_line_item().
 */
function uc_giftwrap_uc_line_item() {
  $items[] = array(
    'id' => 'gift_wrap',
    'title' => t('Gift Wrapping'),
    'weight' => 2,
    'default' => FALSE,
    'stored' => TRUE,
    'add_list' => TRUE,
    'calculated' => TRUE,
    'callback' => 'uc_giftwrap_uc_line_item_gift_wrap',
    'display_only' => FALSE
  );

  return $items;
}

/**
 * Callback for line item type.
 */
function uc_giftwrap_uc_line_item_gift_wrap($op, $arg1) {
  // Required callback for hook_uc_line_item()
}

/**
 * Implements hook_uc_checkout_complete().
 */
function uc_giftwrap_uc_checkout_complete($order, $account) {
  if (isset($_SESSION['uc_giftwrap_checkout'])) {
    unset($_SESSION['uc_giftwrap_checkout']);
  }
}
15
  • Thank you so much! Let me try this code! I'll let you know of the outcome Mar 15, 2012 at 13:29
  • Followed ur code. Successfully added permissions, admin form, checkout pane but the line item doesn't add to the order. Also the following callback function misses the definition: uc_giftwrap_uc_line_item_gift_wrap Mar 15, 2012 at 14:59
  • @kiamlaluno: please see the comment above. Mar 15, 2012 at 15:06
  • @MuhammadAnasKhan Uh? :)
    – apaderno
    Mar 15, 2012 at 15:10
  • 1
    @MuhammadAnasKhan I get the function is required from Ubercart, but the function is empty because there is no code to be executed from that function, which still needs to be present because Ubercart would report an error, or would not work correctly. That is at least what I get, as the code is supposed to be complete. (See the phrase at the beginning, "I've done exactly this before.")
    – apaderno
    Mar 15, 2012 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.