root / drupal7 / sites / all / modules / commerce / modules / price / commerce_price.api.php @ b858700c
1 |
<?php
|
---|---|
2 |
|
3 |
/**
|
4 |
* @file
|
5 |
* Hooks provided by the Price module.
|
6 |
*/
|
7 |
|
8 |
|
9 |
/**
|
10 |
* Defines options for the calculation setting of a price field display formatter.
|
11 |
*
|
12 |
* @param $field
|
13 |
* The field info array the display formatter is for.
|
14 |
* @param $instance
|
15 |
* The instance info array the display formatter is for.
|
16 |
* @param $view_mode
|
17 |
* The view mode whose display formatter settings should be used on render.
|
18 |
*
|
19 |
* @return
|
20 |
* An array of key / value pairs for use in a select list options array.
|
21 |
*/
|
22 |
function hook_commerce_price_field_calculation_options($field, $instance, $view_mode) { |
23 |
// This example from the Product Pricing module adds an option for the display
|
24 |
// formatter to specify the calculated sell price for use in the display.
|
25 |
|
26 |
// If this is a single value purchase price field attached to a product...
|
27 |
if (($instance['entity_type'] == 'commerce_product' || $field['entity_types'] == array('commerce_product')) && |
28 |
$field['field_name'] == 'commerce_price' && $field['cardinality'] == 1) { |
29 |
return array('calculated_sell_price' => t('Display the calculated sell price for the current user.')); |
30 |
} |
31 |
} |
32 |
|
33 |
/**
|
34 |
* Defines price component types for use in price component arrays.
|
35 |
*
|
36 |
* The price field data array includes a components array that keeps track of
|
37 |
* the various components of a price that result in the price field's current
|
38 |
* amount. A price field's amount column reflects the sum of all of its
|
39 |
* components. Each component includes a component type and a price array
|
40 |
* representing the amount, currency code, and data of the component.
|
41 |
*
|
42 |
* The Price module defines three default price component types:
|
43 |
* - Base price: generally used to represent a product's base price as derived
|
44 |
* from the product itself and manipulated by Rules; appears in price
|
45 |
* component lists as the Subtotal
|
46 |
* - Discount: used for generic discounts applied by Rules
|
47 |
* - Fee: used for generic fees applied by Rules
|
48 |
*
|
49 |
* The Tax module also defines a price component type for each tax rate that
|
50 |
* requests it.
|
51 |
*
|
52 |
* The price component type array structure includes the following keys:
|
53 |
* - name: the machine-name of the price component type
|
54 |
* - title: the translatable title of the price component for use in
|
55 |
* administrative displays
|
56 |
* - display_title: the translatable display title of the price component for
|
57 |
* use in front end display; defaults to the title
|
58 |
* - weight: the sort order of the price component type for use in listings of
|
59 |
* combined price components contained in a price's components array
|
60 |
*
|
61 |
* @return
|
62 |
* An array of price component type arrays keyed by name.
|
63 |
*/
|
64 |
function hook_commerce_price_component_type_info() { |
65 |
return array( |
66 |
'base_price' => array( |
67 |
'title' => t('Base price'), |
68 |
'display_title' => t('Subtotal'), |
69 |
'weight' => -50, |
70 |
), |
71 |
'discount' => array( |
72 |
'title' => t('Discount'), |
73 |
'weight' => -10, |
74 |
), |
75 |
'fee' => array( |
76 |
'title' => t('Fee'), |
77 |
'weight' => -20, |
78 |
), |
79 |
); |
80 |
} |
81 |
|
82 |
/**
|
83 |
* Allows modules to alter the price component types defined by other modules.
|
84 |
*
|
85 |
* @param $component_types
|
86 |
* The array of price component types defined by enabled modules.
|
87 |
*/
|
88 |
function hook_commerce_price_component_type_info_alter(&$component_types) { |
89 |
// No example.
|
90 |
} |
91 |
|
92 |
/**
|
93 |
* Functions as a secondary hook_field_formatter_prepare_view() for price fields,
|
94 |
* allowing modules to alter prices prior to display.
|
95 |
*
|
96 |
* This hook is used by modules like the Product Pricing module that implement
|
97 |
* ways to alter prices prior to display. Modules implementing this hook are
|
98 |
* currently responsible to make sure they do not alter price data twice on the
|
99 |
* same pageload.
|
100 |
*/
|
101 |
function hook_commerce_price_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, $items, $displays) { |
102 |
static $calculated_prices = array(); |
103 |
|
104 |
// If this is a single value purchase price field attached to a product...
|
105 |
if ($entity_type == 'commerce_product' && $field['field_name'] == 'commerce_price' && $field['cardinality'] == 1) { |
106 |
// Prepare the items for each entity passed in.
|
107 |
foreach ($entities as $product_id => $product) { |
108 |
// If this price should be calculated and hasn't been already...
|
109 |
if (!empty($displays[$product_id]['settings']['calculation']) && |
110 |
$displays[$product_id]['settings']['calculation'] == 'calculated_sell_price' && |
111 |
empty($calculated_prices[$product_id][$field['field_name']])) { |
112 |
// Replace the data being displayed with data from a calculated price.
|
113 |
$items[$product_id] = array(commerce_product_calculate_sell_price($product)); |
114 |
|
115 |
// Keep track of which prices have already been calculated.
|
116 |
$calculated_prices[$product_id][$field['field_name']] = TRUE; |
117 |
} |
118 |
} |
119 |
} |
120 |
} |
121 |
|
122 |
/**
|
123 |
* Lets modules alter price components prior to display through the "Formatted
|
124 |
* amount with components" display formatter.
|
125 |
*
|
126 |
* @param &$components
|
127 |
* The array of totaled price components.
|
128 |
* @param $price
|
129 |
* The price array the components came from.
|
130 |
* @param $entity
|
131 |
* The entity the price belongs to.
|
132 |
*
|
133 |
* @see commerce_price_field_formatter_view()
|
134 |
*/
|
135 |
function hook_commerce_price_formatted_components_alter(&$components, $price, $entity) { |
136 |
// No example.
|
137 |
} |