Commit 3236fb71 authored by rfay's avatar rfay

Issue #1328388 by rfay: Add a more extensive line item type with callbacks

parent c1410762
...@@ -13,4 +13,17 @@ ...@@ -13,4 +13,17 @@
function line_item_example_enable() { function line_item_example_enable() {
// This function configures all existing line item types, including ours. // This function configures all existing line item types, including ours.
commerce_line_item_configure_line_item_types(); commerce_line_item_configure_line_item_types();
}
/**
* Implements hook_uninstall().
*/
function line_item_example_uninstall() {
$variables = array(
'line_item_example_line_item_2_amount',
'line_item_example_line_item_2_currency',
);
foreach ($variables as $variable) {
variable_del($variable);
}
} }
\ No newline at end of file
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
/** /**
* @file line_item_example.module * @file line_item_example.module
* Demonstrates the addition of a new pane to the checkout system. * Demonstrates the addition of a new pane to the checkout system.
*
*/ */
/** /**
...@@ -32,24 +33,213 @@ function line_item_example_info_page() { ...@@ -32,24 +33,213 @@ function line_item_example_info_page() {
/** /**
* Implements hook_commerce_line_item_type_info(). * Implements hook_commerce_line_item_type_info().
* *
* - 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.
*
* @see hook_commerce_line_item_type_info(). * @see hook_commerce_line_item_type_info().
* @see http://www.drupalcommerce.org/specification/info-hooks/checkout * @see http://www.drupalcommerce.org/specification/info-hooks/checkout
* *
*/ */
function line_item_example_commerce_line_item_type_info() { function line_item_example_commerce_line_item_type_info() {
$line_item_types['example_line_item'] = array( $line_item_types['example_line_item_1'] = array(
'name' => t('Example Line Item'), 'name' => t('Example Line Item 1'),
'description' => t('Example Line Item type of line item'), 'description' => t('Simplest Example Line Item Type'),
'product' => TRUE, 'product' => TRUE,
// Here you can change the text in the submit button in the add to cart form // Here you can change the text in the submit button in the order admin form.
'add_form_submit_value' => t('Add product'), 'add_form_submit_value' => t('Add product'),
// '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.
'base' => 'commerce_product_line_item', 'base' => 'commerce_product_line_item',
); );
$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',
),
);
return $line_item_types; return $line_item_types;
} }
/**
* 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);
}
}
/** /**
* Implements hook_commerce_line_item_type_info_alter(). * Implements hook_commerce_line_item_type_info_alter().
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment