root / drupal7 / sites / all / modules / commerce / modules / checkout / commerce_checkout.api.php @ b858700c
1 |
<?php
|
---|---|
2 |
|
3 |
/**
|
4 |
* @file
|
5 |
* Hooks provided by the Checkout module.
|
6 |
*/
|
7 |
|
8 |
|
9 |
/**
|
10 |
* Routes checkout/%commerce_order* to an alternate URL if necessary.
|
11 |
*
|
12 |
* The checkout module uses two URLs for the checkout form, displaying a form
|
13 |
* specific to the checkout page indicated by the URL.
|
14 |
*
|
15 |
* - checkout/%commerce_order: used for the first checkout page
|
16 |
* - checkout/%commerce_order/%commerce_checkout_page: used for all subsequent
|
17 |
* checkout pages
|
18 |
*
|
19 |
* The page callback for these URLs checks the user's access to the requested
|
20 |
* checkout page for the given order and to make sure the order has line items.
|
21 |
* After these two checks, it gives other modules an opportunity to evaluate the
|
22 |
* order and checkout page to see if any other redirection is necessary. This
|
23 |
* hook should not be used to alter the output at the actual checkout URL.
|
24 |
*
|
25 |
* @param $order
|
26 |
* The order object specified by the checkout URL.
|
27 |
* @param $checkout_page
|
28 |
* The checkout page array specified by the checkout URL.
|
29 |
*
|
30 |
* @see commerce_checkout_router()
|
31 |
*/
|
32 |
function hook_commerce_checkout_router($order, $checkout_page) { |
33 |
global $user; |
34 |
|
35 |
// Redirect anonymous users to a custom login page instructing them to login
|
36 |
// prior to checkout. (Note that Drupal Commerce does not require users to
|
37 |
// login prior to checkout as an e-commerce best practice.)
|
38 |
if (!$user->uid) { |
39 |
drupal_set_message(t('Please login or create an account now to continue checkout.'));
|
40 |
drupal_goto('checkout/login/' . $order->order_id); |
41 |
} |
42 |
} |
43 |
|
44 |
/**
|
45 |
* Allows modules to confirm that an order may proceed to checkout.
|
46 |
*
|
47 |
* If any implementation of this hook returns TRUE, the given order can proceed
|
48 |
* to checkout. However, if no implementations of this hook exist and return
|
49 |
* TRUE, the checkout router will simply redirect away to the front page.
|
50 |
*
|
51 |
* @param $order
|
52 |
* The order being confirmed for checkout.
|
53 |
*
|
54 |
* @return
|
55 |
* Boolean value indicating whether or not the order can proceed to checkout.
|
56 |
*/
|
57 |
function hook_commerce_checkout_order_can_checkout($order) { |
58 |
// Allow orders with one or more product line items to proceed to checkout.
|
59 |
// If there are no line items on the order, redirect away.
|
60 |
$wrapper = entity_metadata_wrapper('commerce_order', $order); |
61 |
|
62 |
if (commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types()) > 0) { |
63 |
return TRUE; |
64 |
} |
65 |
} |
66 |
|
67 |
/**
|
68 |
* Allows modules to perform business logic when an order completes checkout.
|
69 |
*
|
70 |
* This hook coincides with the "Customer completes checkout" event. Only
|
71 |
* business logic should be performed when this is invoked, such as updating the
|
72 |
* order status, assigning the order to a user account, or sending notification
|
73 |
* e-mails. Interaction with the user should instead occur through checkout
|
74 |
* panes on the checkout completion page.
|
75 |
*
|
76 |
* @param $order
|
77 |
* The order that just completed checkout.
|
78 |
*/
|
79 |
function hook_commerce_checkout_complete($order) { |
80 |
// No example.
|
81 |
} |
82 |
|
83 |
/**
|
84 |
* Defines checkout pages available for use in the checkout process.
|
85 |
*
|
86 |
* The checkout form is not a true multi-step form in the Drupal sense, but it
|
87 |
* does use a series of connected menu items and the same form builder function
|
88 |
* to present the contents of each checkout page. Furthermore, as the customer
|
89 |
* progresses through checkout, their order’s status will be updated to reflect
|
90 |
* their current page in checkout.
|
91 |
*
|
92 |
* The Checkout module defines several checkout pages in its own implementation
|
93 |
* of this hook, commerce_checkout_commerce_checkout_page_info():
|
94 |
* - Checkout: the first page where the customer will enter their basic order
|
95 |
* information
|
96 |
* - Review: a page where they can verify that the details of their order are
|
97 |
* correct (and the default location of the payment checkout pane if the
|
98 |
* Payment module is enabled)
|
99 |
* - Complete - the final step in checkout displaying pertinent order details
|
100 |
* and links
|
101 |
*
|
102 |
* The Payment module adds an additional page:
|
103 |
* - Payment: a page that only appears when the customer selected an offsite
|
104 |
* payment method; the related checkout pane handles building the form and
|
105 |
* automatically submitting it to send the customer to the payment provider
|
106 |
*
|
107 |
* The checkout page array contains properties that define how the page should
|
108 |
* interact with the shopping cart and order status systems. It also contains
|
109 |
* properties that define the appearance and use of buttons on the page.
|
110 |
*
|
111 |
* The checkout page array structure is as follows:
|
112 |
* - page_id: machine-name identifying the page using lowercase alphanumeric
|
113 |
* characters, -, and _
|
114 |
* - title: the Drupal page title used for this checkout page
|
115 |
* - name: the translatable name of the page, used in administrative displays
|
116 |
* and the page’s corresponding order status; if not specified, defaults to
|
117 |
* the title
|
118 |
* - help: the translatable help text displayed in a .checkout-help div at the
|
119 |
* top of the checkout page (defined as part of the form array, not displayed
|
120 |
* via hook_help())
|
121 |
* - weight: integer weight of the page used for determining the page order;
|
122 |
* populated automatically if not specified
|
123 |
* - status_cart: boolean indicating whether or not this page’s corresponding
|
124 |
* order status should be considered a shopping cart order status (this is
|
125 |
* necessary because the shopping cart module relies on order status to
|
126 |
* identify the user’s current shopping cart); defaults to TRUE
|
127 |
* - buttons - boolean indicating whether or not the checkout page should have
|
128 |
* buttons for continuing and going back in the checkout process; defaults to
|
129 |
* TRUE
|
130 |
* - back_value: the translatable value of the submit button used for going back
|
131 |
* to a previous page in the checkout process, which is different from the
|
132 |
* cancel button used to exit the checkout process from the first checkout
|
133 |
* page; defaults to ‘Go back’
|
134 |
* - submit_value: the translatable value of the submit button used for going
|
135 |
* forward in the checkout process; defaults to ‘Continue’
|
136 |
* - prev_page: the page_id of the previous page in the checkout process; should
|
137 |
* not be set by the hook but will be populated automatically when the page is
|
138 |
* loaded
|
139 |
* - next_page: the page_id of the next page in the checkout process; should not
|
140 |
* be set by the hook but will be populated automatically when the page is
|
141 |
* loaded
|
142 |
*
|
143 |
* Note: At this point there is no way to add checkout pages via the UI, so
|
144 |
* sites wishing to add extra steps to the checkout process will need to define
|
145 |
* custom pages.
|
146 |
*
|
147 |
* @return
|
148 |
* An array of checkout page arrays keyed by page_id.
|
149 |
*/
|
150 |
function hook_commerce_checkout_page_info() { |
151 |
$checkout_pages = array(); |
152 |
|
153 |
$checkout_pages['complete'] = array( |
154 |
'name' => t('Complete'), |
155 |
'title' => t('Checkout complete'), |
156 |
'weight' => 50, |
157 |
'status_cart' => FALSE, |
158 |
'buttons' => FALSE, |
159 |
); |
160 |
|
161 |
return $checkout_pages; |
162 |
} |
163 |
|
164 |
/**
|
165 |
* Allows modules to alter checkout pages defined by other modules.
|
166 |
*
|
167 |
* @param $checkout_pages
|
168 |
* The array of checkout page arrays.
|
169 |
*
|
170 |
* @see hook_commerce_checkout_page_info()
|
171 |
*/
|
172 |
function hook_commerce_checkout_page_info_alter(&$checkout_pages) { |
173 |
$checkout_pages['review']['weight'] = 15; |
174 |
} |
175 |
|
176 |
/**
|
177 |
* Defines checkout panes available for use on checkout pages.
|
178 |
*
|
179 |
* Any number of panes may be assigned to a page and reordered using the
|
180 |
* checkout form builder. Each pane may also have its own settings form
|
181 |
* accessible from the builder. On the checkout page, a pane is represented as a
|
182 |
* fieldset or container div. Panes possess a variety of callbacks used to
|
183 |
* define settings and checkout form elements and validate / process submitted
|
184 |
* data when the checkout form is submitted.
|
185 |
*
|
186 |
* The Checkout module defines a couple of checkout panes in its own
|
187 |
* implementation of this hook, commerce_checkout_commerce_checkout_pane_info():
|
188 |
* - Review: the main pane on the default Review page that displays details from
|
189 |
* other checkout panes for the user to review prior to completion
|
190 |
* - Completion message: the main pane on the default Complete page that
|
191 |
* displays the checkout completion message and links
|
192 |
*
|
193 |
* Other checkout panes are defined by the Cart, Customer, and Payment modules
|
194 |
* as follows:
|
195 |
* - Shopping cart contents: displays a View listing the contents of the
|
196 |
* shopping cart order with a summary including the total cost and number of
|
197 |
* items but no links (as used in the cart block)
|
198 |
* - Customer profile panes: the Customer module defines one for each type of
|
199 |
* customer information profile using the name of the profile type as the
|
200 |
* title of the pane
|
201 |
* - Payment: the main payment pane that lets the customer select a payment
|
202 |
* method and supply any necessary payment details; appears on the Review page
|
203 |
* beneath the Review pane by default, allowing payments to be processed
|
204 |
* immediately on submission for security purposes
|
205 |
* - Off-site payment redirect: a pane that handles redirected payment services
|
206 |
* with some specialized behavior; should be the only pane on the actual
|
207 |
* payment page
|
208 |
*
|
209 |
* The checkout pane array contains properties that directly affect the pane’s
|
210 |
* fieldset display on the checkout form. It also contains a property used to
|
211 |
* automatically populate an array of callback function names.
|
212 |
*
|
213 |
* The full list of properties is as follows:
|
214 |
* - pane_id: machine-name identifying the pane using lowercase alphanumeric
|
215 |
* characters, -, and _
|
216 |
* - title: the translatable title used for this checkout pane as the fieldset
|
217 |
* title in checkout
|
218 |
* - name: the translatable name of the pane, used in administrative displays;
|
219 |
* if not specified, defaults to the title
|
220 |
* - page: the page_id of the checkout page the pane should appear on by
|
221 |
* default; defaults to ‘checkout’
|
222 |
* - locked: boolean indicating that the pane cannot be moved from the
|
223 |
* specified checkout page.
|
224 |
* - collapsible: boolean indicating whether or not the checkout pane’s fieldset
|
225 |
* should be collapsible; defaults to FALSE
|
226 |
* - collapsed: boolean indicating whether or not the checkout pane’s fieldset
|
227 |
* should be collapsed by default; defaults to FALSE
|
228 |
* - weight: integer weight of the page used for determining the pane sort order
|
229 |
* on checkout pages; defaults to 0
|
230 |
* - enabled: boolean indicating whether or not the pane is enabled by default;
|
231 |
* defaults to TRUE
|
232 |
* - review: boolean indicating whether or not the pane should be included in
|
233 |
* the review checkout pane; defaults to TRUE
|
234 |
* - module: the name of the module that defined the pane; should not be set by
|
235 |
* the hook but will be populated automatically when the pane is loaded
|
236 |
* - file: the filepath of an include file relative to the pane’s module
|
237 |
* containing the callback functions for this pane, allowing modules to store
|
238 |
* checkout pane code in include files that only get loaded when necessary
|
239 |
* (like the menu item file property)
|
240 |
* - base: string used as the base for the magically constructed callback names,
|
241 |
* each of which will be defaulted to [base]_[callback] unless explicitly set;
|
242 |
* defaults to the pane_id
|
243 |
* - callbacks: an array of callback function names for the various types of
|
244 |
* callback required for all the checkout pane operations, arguments per
|
245 |
* callback in parentheses:
|
246 |
* - settings_form($checkout_pane): returns form elements for the pane’s
|
247 |
* settings form
|
248 |
* - checkout_form($form, &$form_state, $checkout_pane, $order): returns form
|
249 |
* elements for the pane’s checkout form fieldset
|
250 |
* - checkout_form_validate($form, &$form_state, $checkout_pane, $order):
|
251 |
* validates data inputted via the pane’s elements on the checkout form and
|
252 |
* must return TRUE or FALSE indicating whether or not all the data validated
|
253 |
* - checkout_form_submit($form, &$form_state, $checkout_pane, $order):
|
254 |
* processes data inputted via the pane’s elements on the checkout form,
|
255 |
* often updating parts of the order object based on the data
|
256 |
* - review($form, $form_state, $checkout_pane, $order): returns data used in
|
257 |
* the construction of the Review checkout pane
|
258 |
*
|
259 |
* The helper function commerce_checkout_pane_callback() will include a checkout
|
260 |
* pane’s include file if specified and check for the existence of a callback,
|
261 |
* returning either the name of the function or FALSE if the specified callback
|
262 |
* does not exist for the specified pane.
|
263 |
*
|
264 |
* @return
|
265 |
* An array of checkout pane arrays keyed by pane_id.
|
266 |
*
|
267 |
* @see commerce_checkout_pane_callback()
|
268 |
*/
|
269 |
function hook_commerce_checkout_pane_info() { |
270 |
$checkout_panes = array(); |
271 |
|
272 |
$checkout_panes['checkout_review'] = array( |
273 |
'title' => t('Review'), |
274 |
'file' => 'includes/commerce_checkout.checkout_pane.inc', |
275 |
'base' => 'commerce_checkout_review_pane', |
276 |
'page' => 'review', |
277 |
'fieldset' => FALSE, |
278 |
'locked' => FALSE, |
279 |
); |
280 |
|
281 |
return $checkout_panes; |
282 |
} |
283 |
|
284 |
/**
|
285 |
* Allows modules to alter checkout panes defined by other modules.
|
286 |
*
|
287 |
* @param $checkout_panes
|
288 |
* The array of checkout pane arrays.
|
289 |
*
|
290 |
* @see hook_commerce_checkout_pane_info()
|
291 |
*/
|
292 |
function hook_commerce_checkout_pane_info_alter(&$checkout_panes) { |
293 |
$checkout_panes['billing']['weight'] = -6; |
294 |
} |