web-dev-qa-db-ja.com

Ubercart 3でラインアイテムを作成する

注文に合わせてカスタム料金(ギフト包装料金など)を追加するモジュールを作成する必要があります。

Drupal 7.を使用しています。

3

私はこれを正確に以前にやったことがあります。使用したコードをダンプします。うまくいけば、それはあなたにアイデアを与えるでしょう。

/**
 * 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']);
  }
}
6
Clive