Newer
Older
Ryan Szrama
committed
<?php
/**
* @file
* Hooks provided by the Price module.
*/
Ryan Szrama
committed
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Defines options for the calculation setting of a price field display formatter.
*
* @param $field
* The field info array the display formatter is for.
* @param $instance
* The instance info array the display formatter is for.
* @param $view_mode
* The view mode whose display formatter settings should be used on render.
*
* @return
* An array of key / value pairs for use in a select list options array.
*/
function hook_commerce_price_field_calculation_options($field, $instance, $view_mode) {
// This example from the Product Pricing module adds an option for the display
// formatter to specify the calculated sell price for use in the display.
// If this is a single value purchase price field attached to a product...
if (($instance['entity_type'] == 'commerce_product' || $field['entity_types'] == array('commerce_product')) &&
$field['field_name'] == 'commerce_price' && $field['cardinality'] == 1) {
return array('calculated_sell_price' => t('Display the calculated sell price for the current user.'));
}
}
/**
* Defines price component types for use in price component arrays.
*
* The price field data array includes a components array that keeps track of
* the various components of a price that result in the price field's current
* amount. A price field's amount column reflects the sum of all of its
* components. Each component includes a component type and a price array
* representing the amount, currency code, and data of the component.
*
* The Price module defines three default price component types:
* - Base price: generally used to represent a product's base price as derived
* from the product itself and manipulated by Rules; appears in price
* component lists as the Subtotal
* - Discount: used for generic discounts applied by Rules
* - Fee: used for generic fees applied by Rules
*
* The Tax module also defines a price component type for each tax rate that
* requests it.
*
* The price component type array structure includes the following keys:
* - name: the machine-name of the price component type
* - title: the translatable title of the price component for use in
* administrative displays
* - display_title: the translatable display title of the price component for
* use in front end display; defaults to the title
* - weight: the sort order of the price component type for use in listings of
* combined price components contained in a price's components array
*
* @return
* An array of price component type arrays keyed by name.
*/
function hook_commerce_price_component_type_info() {
Ryan Szrama
committed
return array(
Ryan Szrama
committed
'base_price' => array(
'title' => t('Base price'),
'display_title' => t('Subtotal'),
'weight' => -50,
),
'discount' => array(
'title' => t('Discount'),
'weight' => -10,
),
'fee' => array(
'title' => t('Fee'),
'weight' => -20,
),
);
}
/**
* Allows modules to alter the price component types defined by other modules.
*
* @param $component_types
* The array of price component types defined by enabled modules.
*/
function hook_commerce_price_component_type_info_alter(&$component_types) {
// No example.
}
Ryan Szrama
committed
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
/**
* Functions as a secondary hook_field_formatter_prepare_view() for price fields,
* allowing modules to alter prices prior to display.
*
* This hook is used by modules like the Product Pricing module that implement
* ways to alter prices prior to display. Modules implementing this hook are
* currently responsible to make sure they do not alter price data twice on the
* same pageload.
*/
function hook_commerce_price_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, $items, $displays) {
static $calculated_prices = array();
// If this is a single value purchase price field attached to a product...
if ($entity_type == 'commerce_product' && $field['field_name'] == 'commerce_price' && $field['cardinality'] == 1) {
// Prepare the items for each entity passed in.
foreach ($entities as $product_id => $product) {
// If this price should be calculated and hasn't been already...
if (!empty($displays[$product_id]['settings']['calculation']) &&
$displays[$product_id]['settings']['calculation'] == 'calculated_sell_price' &&
empty($calculated_prices[$product_id][$field['field_name']])) {
// Replace the data being displayed with data from a calculated price.
$items[$product_id] = array(commerce_product_calculate_sell_price($product));
// Keep track of which prices have already been calculated.
$calculated_prices[$product_id][$field['field_name']] = TRUE;
}
}
}
}
Ryan Szrama
committed
/**
* Lets modules alter price components prior to display through the "Formatted
* amount with components" display formatter.
*
* @param &$components
* The array of totaled price components.
* @param $price
* The price array the components came from.
* @param $entity
* The entity the price belongs to.
Ryan Szrama
committed
*
* @see commerce_price_field_formatter_view()
*/
function hook_commerce_price_formatted_components_alter(&$components, $price, $entity) {
Ryan Szrama
committed
// No example.
}