commerce_shipping_example.module in Commerce Shipping 7.2
Same filename and directory in other branches
Defines an example shipping method for testing and development.
File
modules/commerce_shipping_example.moduleView source
<?php
/**
* @file
* Defines an example shipping method for testing and development.
*/
/**
* Implements hook_commerce_shipping_method_info().
*/
function commerce_shipping_example_commerce_shipping_method_info() {
$shipping_methods = array();
$shipping_methods['example_shipping_method'] = array(
'title' => t('Example shipping method'),
'description' => t('Defines a single flat rate service with a couple of service details.'),
);
return $shipping_methods;
}
/**
* Implements hook_commerce_shipping_service_info().
*/
function commerce_shipping_example_commerce_shipping_service_info() {
$shipping_services = array();
$shipping_services['example_shipping_service'] = array(
'title' => t('Example shipping service'),
'description' => t('A flat rate service with express delivery available.'),
'display_title' => t('Shipping'),
'shipping_method' => 'example_shipping_method',
'price_component' => 'shipping',
'callbacks' => array(
'rate' => 'commerce_shipping_example_service_rate',
'details_form' => 'commerce_shipping_example_service_details_form',
'details_form_validate' => 'commerce_shipping_example_service_details_form_validate',
'details_form_submit' => 'commerce_shipping_example_service_details_form_submit',
),
);
return $shipping_services;
}
/**
* Shipping service callback.
*
* Returns a base price array for a shipping service
* calculated for the given order.
*/
function commerce_shipping_example_service_rate($shipping_service, $order) {
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
return array(
'amount' => 2000,
'currency_code' => $order_wrapper->commerce_order_total->currency_code
->value(),
'data' => array(),
);
}
/**
* Shipping service callback: returns the example shipping service details form.
*/
function commerce_shipping_example_service_details_form($pane_form, $pane_values, $checkout_pane, $order, $shipping_service) {
$form = array();
$pane_values['service_details'] += array(
'name' => '',
'express' => '',
);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#description' => t('This is a demonstration field coded to fail validation for single character values.'),
'#default_value' => $pane_values['service_details']['name'],
'#required' => TRUE,
);
$form['express'] = array(
'#type' => 'checkbox',
'#title' => t('Express delivery'),
'#description' => t('Express delivery is really fast and you need to pay extra for this service.'),
'#default_value' => $pane_values['service_details']['express'],
);
return $form;
}
/**
* Shipping service callback: validates the example shipping service details.
*/
function commerce_shipping_example_service_details_form_validate($details_form, $details_values, $shipping_service, $order, $form_parents) {
if (strlen($details_values['name']) < 2) {
form_set_error(implode('][', array_merge($form_parents, array(
'name',
))), t('You must enter a name two or more characters long.'));
// Even though the form error is enough to stop the submission of the form,
// it's not enough to stop it from a Commerce standpoint because of the
// combined validation / submission going on per-pane in the checkout form.
return FALSE;
}
}
/**
* Shipping service callback.
*
* Increases the shipping line item's unit price if
* express delivery was selected.
*/
function commerce_shipping_example_service_details_form_submit($details_form, $details_values, $line_item) {
if ($details_values['express']) {
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
// Build a price array for the express delivery fee.
$express_price = array(
'amount' => 1500,
'currency_code' => $line_item_wrapper->commerce_unit_price->currency_code
->value(),
'data' => array(),
);
// Add the express upcharge to the line item unit price.
$line_item_wrapper->commerce_unit_price->amount = $line_item_wrapper->commerce_unit_price->amount
->value() + 1500;
// Add the express delivery fee component to the unit price.
$line_item_wrapper->commerce_unit_price->data = commerce_price_component_add($line_item_wrapper->commerce_unit_price
->value(), 'example_shipping_service_express', $express_price, TRUE, FALSE);
}
}
/**
* Implements hook_commerce_price_component_type_info().
*/
function commerce_shipping_example_commerce_price_component_type_info() {
return array(
'example_shipping_service_express' => array(
'title' => t('Express delivery'),
'weight' => 20,
),
);
}
Functions
Name | Description |
---|---|
commerce_shipping_example_commerce_price_component_type_info | Implements hook_commerce_price_component_type_info(). |
commerce_shipping_example_commerce_shipping_method_info | Implements hook_commerce_shipping_method_info(). |
commerce_shipping_example_commerce_shipping_service_info | Implements hook_commerce_shipping_service_info(). |
commerce_shipping_example_service_details_form | Shipping service callback: returns the example shipping service details form. |
commerce_shipping_example_service_details_form_submit | Shipping service callback. |
commerce_shipping_example_service_details_form_validate | Shipping service callback: validates the example shipping service details. |
commerce_shipping_example_service_rate | Shipping service callback. |