Commit 9ed8dcca authored by rfay's avatar rfay

Issue #1359900 by rfay: Create a price example

parent 2233497f
name = Price Example
description = Demonstrates pricing hooks
package = Commerce (contrib)
dependencies[] = commerce_price
dependencies[] = commerce_cart
dependencies[] = commerce_product_ui
dependencies[] = commerce_product
dependencies[] = product_example
dependencies[] = devel
core = 7.x
<?php
/**
* @file price_example.install
*/
/**
* Implements hook_uninstall().
*/
function price_example_uninstall() {
$variables = array(
'price_example_product',
);
foreach ($variables as $variable) {
variable_del($variable);
}
}
\ No newline at end of file
<?php
/**
* @file price_example.module
* Demonstrates pricing hooks, etc.
*/
/**
* Implements hook_menu().
*
* Simply presents a page that will explain what this module is for.
* hook_menu() has nothing to do with the checkout page functionality.
*/
function price_example_menu() {
$items['commerce_examples/price_example'] = array(
'title' => 'Price Example',
'page callback' => 'drupal_get_form',
'page arguments' => array('price_example_info_page'),
'access callback' => TRUE,
);
return $items;
}
/**
* This form provides a way to interact with some of the example functions
* provided here.
*/
function price_example_info_page($form, &$form_state) {
$form['explanation'] = array(
'#type' => 'item',
'#markup' => t('This example demonstrates price components and price formatting.'),
);
$components = commerce_price_component_types();
$form['components'] = array(
'#type' => 'fieldset',
'#title' => t('Available price components'),
'#description' => t('These components are currently defined on this system; we can get these from commerce_price_component_types(). They are provided by modules that implement hook_commerce_price_component_type_info().') . kpr($components, TRUE),
);
$product_id = variable_get('price_example_product', 0);
$product = commerce_product_load($product_id);
if (empty($product)) {
$form['product_creation'] = array(
'#type' => 'fieldset',
'#title' => t('Please create a product for use with this example'),
);
$types = commerce_product_types();
$form['product_creation']['product_type'] = array(
'#type' => 'select',
'#title' => t('Product type for product to be created'),
'#options' => drupal_map_assoc(array_keys($types)),
);
$form['product_creation']['title'] = array(
'#type' => t('Product title'),
'#type' => 'textfield',
'#default_value' => t('A dummy product for use with price_example'),
);
$form['product_creation']['price'] = array(
'#type' => t('Product price'),
'#type' => 'textfield',
'#description' => t('A price in decimal format, without a currency symbol'),
'#default_value' => '100.00',
);
$form['product_creation']['product_creation_submit'] = array(
'#type' => 'submit',
'#value' => t('Create product'),
'#submit' => array('price_example_product_creation_submit')
);
}
else {
$form['product_pricing'] = array(
'#type' => 'fieldset',
'#title' => t('Product Pricing Example'),
'#description' => t('You are using product !product for pricing. You can explore the components in the calculated price of this product below. Taxes are normally positive (they add to the price) and discounts are normally negative (they subtract from the price).', array('!product' => l($product->title, 'admin/commerce/products/' . $product->product_id))),
);
$sell_price = commerce_product_calculate_sell_price($product);
$precalculated_sell_price = commerce_product_calculate_sell_price($product, TRUE);
$components = $sell_price['data']['components'];
$form['product_pricing']['components'] = array(
'#type' => 'item',
'#title' => t('Components for sample product'),
'#markup' => kpr($components, TRUE),
);
}
return $form;
}
/**
* Submit handler for creating a product.
*
* We need a sample product so we can show price calculation using it.
*/
function price_example_product_creation_submit($form, &$form_state) {
$extras = array(
'sku' => 'price_example_' . drupal_hash_base64(microtime()),
'status' => TRUE,
'uid' => $GLOBALS['user']->uid,
'title' => $form_state['values']['title'],
);
// Use the product example's creation function to create a product.
$product_id = product_example_create_product($form_state['values']['product_type'], $form_state['values']['price'], $extras);
// Save it as our sample to use until it goes away.
variable_set('price_example_product', $product_id);
drupal_set_message(t('Created sample product with title !title and sku !sku', array('!title' => l($extras['title'], 'admin/commerce/products/' . $product_id), '!sku' => $extras['sku'])));
}
/**
* Implements hook_commerce_price_component_type_info()
*
* Provides a new component type. The component type can be used by rules and
* displayed by views.
*
* Components are just buckets where rules can put price differentials so that
* product pricing can properly calculate prices.
*/
function price_example_commerce_price_component_type_info() {
return array(
'price_example_component' => array(
'title' => t('Price Example component'),
'display_title' => t('Price Example component'),
'weight' => 100,
),
);
}
/**
* Implements hook_commerce_price_component_type_info_alter().
*
* @param $components
* The gathered array of components which can now be altered.
*/
function price_example_commerce_price_component_type_info_alter(&$components) {
if (!empty($components['base_price'])) {
$components['base_price']['weight'] = -49;
}
}
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