line_item_example.module 8.75 KB
Newer Older
rfay's avatar
rfay committed
1 2 3 4
<?php
/**
 * @file line_item_example.module
 * Demonstrates the addition of a new pane to the checkout system.
5
 *
rfay's avatar
rfay committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 */

/**
 * Implements hook_menu().
 *
 * Simply presents a page that will explain what this module is for.
 * hook_menu() has nothing to do with the checkout pane functionality.
 */
function line_item_example_menu() {
  $items['commerce_examples/line_item_example'] = array(
    'title' => 'Line Item Example',
    'page callback' => 'line_item_example_info_page',
    'access callback' => TRUE,
  );
  return $items;
}

/**
 * This function simply returns information about the example to help the
 * user understand its purpose.
 */
function line_item_example_info_page() {
  return array(
    '#markup' => t('The line item example demonstrates adding a custom line item and also how to use related API functions. With this module enabled, an additional line item may be selected by add-to-cart forms.'),
  );
}

/**
 * Implements hook_commerce_line_item_type_info().
 *
36 37 38 39 40 41
 * - example_line_item_1 is simply a clone of the 'product' line item type. It's
 *   a simple way to get a differentiated line item type.
 * - example_line_item_2 is completely implemented and is not a product line
 *   item type but rather a "surcharge", and the hook_commerce_order_presave()
 *   is provided to make sure that there is one for every order.
 *
rfay's avatar
rfay committed
42
 * @see hook_commerce_line_item_type_info().
43
 * @see http://www.drupalcommerce.org/specification/info-hooks/checkout
rfay's avatar
rfay committed
44 45 46 47
 *
 */
function line_item_example_commerce_line_item_type_info() {

48 49 50
  $line_item_types['example_line_item_1'] = array(
    'name' => t('Example Line Item 1'),
    'description' => t('Simplest Example Line Item Type'),
rfay's avatar
rfay committed
51 52
    'product' => TRUE,

53
    // Here you can change the text in the submit button in the order admin form.
rfay's avatar
rfay committed
54
    'add_form_submit_value' => t('Add product'),
55 56 57 58 59 60 61 62

    // 'base' basically provides magic naming for a set of callbacks
    // including the settings_form, checkout_form, etc. Here we'll use the
    // forms and callbacks provided for Commerce Product Reference module for
    // its product line item, so we'll use
    // commerce_product_line_item_configuration()
    // and commerce_product_line_item_title(), etc.
    // See example_line_item_2 for explicit callbacks.
rfay's avatar
rfay committed
63 64
    'base' => 'commerce_product_line_item',
  );
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  $line_item_types['example_line_item_2'] = array(
    'name' => t('Example Line Item 2'),
    'description' => t('A more complex line item type, providing explicit callbacks'),
    'product' => FALSE,

    // Here you can change the text in the submit button in the order admin
    // line item add area.
    'add_form_submit_value' => t('Add surcharge'),

    // We could use 'base' to configure the forms we need but instead we'll
    // name the callbacks explicitly.

    // 'base' => 'line_item_example_example_2',

    'callbacks' => array(
      'configuration' => 'line_item_example_example_2_configuration',
      'title' => 'line_item_example_example_2_title',
      'add_form' => 'line_item_example_example_2_add_form',
      'add_form_submit' => 'line_item_example_example_2_add_form_submit',
    ),
  );

rfay's avatar
rfay committed
87 88 89
  return $line_item_types;
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

/**
 * Configure the line item with additional fields or whatever.
 *
 * This function is called by the line item module when it is enabled or this
 * module is enabled. It invokes this function using the configuration_callback
 * as specified above. Other modules defining product line item types should
 * use this function to ensure their types have the required fields.
 *
 * @param $line_item_type
 *   The info array of the line item type being configured.
 *
 * @see commerce_product_line_item_configuration()
 */
function line_item_example_example_2_configuration($line_item_type) {
  $type = $line_item_type['type'];

  // Here we could add fields or other configuration.

}

/**
 * Returns a title for this line item.
 */
function line_item_example_example_2_title($line_item) {
  return(t('Line Item Example 2 Order Surcharge'));
}

/**
 * Returns the elements necessary to add a product line item through a line item
 * manager widget (on the order form).
 */
function line_item_example_example_2_add_form($element, &$form_state) {
  $form = array();

  return $form;
}

/**
 * Adds the selected product information to a line item added via a line item
 *   manager widget (on the admin order page).
 *
 * @param $line_item
 *   The newly created line item object.
 * @param $element
 *   The array representing the widget form element.
 * @param $form_state
 *   The present state of the form upon the latest submission.
 * @param $form
 *   The actual form array.
 *
 * @return
 *   NULL if all is well or an error message if something goes wrong.
 */
function line_item_example_example_2_add_form_submit(&$line_item, $element, &$form_state, $form) {
  $line_item->line_item_label = t('Surcharge');

  // Wrap the line item and product to easily set field information.
  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);

  // Provide a default price.
  $amount = variable_get('line_item_example_line_item_2_amount', 500);
  $currency_code = variable_get('line_item_example_line_item_2_currency', 'USD');

  $line_item->commerce_unit_price = array('und' => array(
    '0' => array('amount' => $amount, 'currency_code' => $currency_code)
  ));

  if (!is_null($line_item_wrapper->commerce_unit_price->value())) {
    // Add the base price to the components array.
    if (!commerce_price_component_load($line_item_wrapper->commerce_unit_price->value(), 'base_price')) {
      $line_item_wrapper->commerce_unit_price->data = commerce_price_component_add(
        $line_item_wrapper->commerce_unit_price->value(),
        'base_price',
        $line_item_wrapper->commerce_unit_price->value(),
        TRUE
      );
    }
  }

}


/**
 * Utility function which creates a new example_2 line item populated with the
 * a price of $5.00, etc..
 *
 * @param $order_id
 *   The ID of the order the line item belongs to (if available).
 *
 * @return
 *   The fully loaded line item..
 */
function line_item_example_line_item_2_new($order_id = 0) {
  $type = 'example_line_item_2';

  // Create the new line item.
  $line_item = entity_create('commerce_line_item', array(
    'type' => $type,
    'order_id' => $order_id,
    'quantity' => 1,
  ));

  // For this example, we'll use a price of USD $5.00 for the "surcharge".
  $amount = variable_get('line_item_example_line_item_2_amount', 500);
  $currency_code = variable_get('line_item_example_line_item_2_currency', 'USD');

  $line_item->commerce_unit_price = array('und' => array(
    '0' => array('amount' => $amount, 'currency_code' => $currency_code)
  ));

  $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);

  if (!is_null($line_item_wrapper->commerce_unit_price->value())) {
    // Add the base price to the components array.
    if (!commerce_price_component_load($line_item_wrapper->commerce_unit_price->value(), 'base_price')) {
      $line_item_wrapper->commerce_unit_price->data = commerce_price_component_add(
        $line_item_wrapper->commerce_unit_price->value(),
        'base_price',
        $line_item_wrapper->commerce_unit_price->value(),
        TRUE
      );
    }
  }
  // Return the line item.
  return $line_item;
}

// There should be one of our example line items on every order, so make sure
// it's there or add it.
function line_item_example_commerce_order_presave($order) {
  // Find out if our order already has an example_2 line item.
  $type_exists = FALSE;
  if (!empty($order->commerce_line_items['und'])) {
    foreach($order->commerce_line_items['und'] as $delta => $line_item_entry) {
      if ($line_item = commerce_line_item_load($line_item_entry['line_item_id'])) {
        if ($line_item->type == 'example_line_item_2') {
          $type_exists = TRUE;
          break;
        }
      }
    }
  }

  // If our line item is not yet in the order and the order has an ID,
  // create a line item to add to the order.
  if (!$type_exists && $order->order_id > 0) {
    $line_item = line_item_example_line_item_2_new($order->order_id);
    commerce_line_item_save($line_item);
    $order->commerce_line_items['und'][] = array('line_item_id' => $line_item->line_item_id);
  }
}

rfay's avatar
rfay committed
243
/**
244 245
 * Implements hook_commerce_line_item_type_info_alter().
 *
rfay's avatar
rfay committed
246 247
 * Note that this only runs when the cache is cleared.
 *
248 249
 * Here we change the friendly name of the "product" line item type to "Widgets"
 *
rfay's avatar
rfay committed
250 251 252 253 254 255 256 257 258 259 260 261 262
 * @see hook_commerce_line_item_type_info_alter().
 *
 * @param $line_item_types
 *   The array of types to be altered
 */
function line_item_example_commerce_line_item_type_info_alter(&$line_item_types) {
  if (!empty($line_item_types['product'])) {
    $line_item_types['product']['name'] = t('Widgets');

    // The add_form_submit_value is only used on the order page.
    $line_item_types['product']['add_form_submit_value'] = t('Buy it now!');
  }
}