commerce_ss.module in Commerce Stock 7.2
Allow commerce products to have stock levels associated with their SKU.
Commerce Stock enables Commerce to manage stock for products. Store admins can set the stock levels on a product edit page.
File
modules/commerce_ss/commerce_ss.moduleView source
<?php
/**
* @file
* Allow commerce products to have stock levels associated with their SKU.
*
* Commerce Stock enables Commerce to manage stock for products. Store admins
* can set the stock levels on a product edit page.
*/
/**
* Implements hook_menu().
*/
function commerce_ss_menu() {
$items = array();
$items['admin/commerce/config/stock/ss'] = array(
'title' => 'Simple Stock management',
'description' => 'Configure Simple stock.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'commerce_ss_admin_form',
),
'access arguments' => array(
'administer commerce_stock settings',
),
'file' => 'includes/commerce_ss.admin.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_entity_property_info_alter().
*/
function commerce_ss_entity_property_info_alter(&$info) {
// Copy metadata about our stock field from the product bundle to the
// commerce_product entity.
if (!empty($info['commerce_product']['bundles'])) {
$properties = array();
foreach ($info['commerce_product']['bundles'] as $bundle => $bundle_info) {
$bundle_info += array(
'properties' => array(),
);
$properties += $bundle_info['properties'];
}
if (isset($properties['commerce_stock'])) {
$info['commerce_product']['properties']['commerce_stock'] = $properties['commerce_stock'];
}
if (isset($properties['commerce_stock_override'])) {
$info['commerce_product']['properties']['commerce_stock_override'] = $properties['commerce_stock_override'];
unset($info['commerce_product']['properties']['commerce_stock_override']['options list']);
}
}
}
/**
* Implements hook_token_info().
*/
function commerce_ss_token_info() {
$info['tokens']['commerce-product']['commerce-stock-int'] = array(
'name' => t('Stock level (int)'),
'description' => t('The stock level as an integer'),
);
$info['tokens']['commerce-product']['commerce-stock-user'] = array(
'name' => t('Stock level available for the user'),
'description' => t('The available stock level for the current user'),
);
return $info;
}
/**
* Implements hook_tokens().
*/
function commerce_ss_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'commerce-product' && !empty($data['commerce-product'])) {
$product = entity_metadata_wrapper('commerce_product', $data['commerce-product']);
foreach ($tokens as $name => $original) {
switch ($name) {
case 'commerce-stock-int':
$replacements[$original] = (int) $product->commerce_stock
->value();
break;
case 'commerce-stock-user':
$replacements[$original] = $product->commerce_stock
->value() - commerce_stock_check_cart_product_level($product->product_id
->value());
break;
}
}
}
return $replacements;
}
/**
* Implements hook_form_ID_alter().
*
* Provides a checkbox on the Product type edit form to allow decimal
* quantities for stock management.
*/
function commerce_ss_form_commerce_product_ui_product_type_form_alter(&$form, &$form_state) {
// Get the product bundle type.
$type = $form_state['product_type']['type'];
// Check if stock enabled for the type.
if (commerce_ss_product_type_enabled($type)) {
$form['product_type']['decimal_admin'] = array(
// Provide a configuration option for decimal stock.
'#type' => 'checkbox',
'#title' => t('Allow decimal quantities for stock administration'),
'#default_value' => variable_get('commerce_stock_decimal_backend_' . $type, FALSE),
'#description' => t('Check this box if you want to allow the users to set decimal stock levels on product edit forms.'),
'#element_validate' => array(
'commerce_ss_checkbox_validate',
),
);
}
}
/**
* Element validation callback for the decimal checkbox.
*/
function commerce_ss_checkbox_validate($element, &$form_state, $form) {
// Get the product bundle type.
$type = $form_state['product_type']['type'];
// Check if stock enabled for the type.
if (commerce_ss_product_type_enabled($type)) {
// Set the configuration option for decimal stock.
variable_set('commerce_stock_decimal_backend_' . $type, $form_state['values']['product_type']['decimal_admin']);
}
}
/**
* Implements hook_form_ID_alter().
*
* Control the decimal quantities option when editing products.
*/
function commerce_ss_form_commerce_product_ui_product_form_alter(&$form, &$form_state) {
// Get the product bundle type.
$type = $form_state['commerce_product']->type;
// Check if stock enabled for the type.
if (commerce_ss_product_type_enabled($type)) {
$decimal = variable_get('commerce_stock_decimal_backend_' . $type, FALSE);
if (!$decimal) {
// Get the fields language.
$language = $form['commerce_stock']['#language'];
// Set the number format to decimal.
$form['commerce_stock'][$language][0]['value']['#number_type'] = 'integer';
$form['commerce_stock'][$language][0]['value']['#default_value'] = intval($form['commerce_stock'][$language][0]['value']['#default_value']);
}
}
}
/**
* Implements hook_inline_entity_form_alter().
*
* Control the decimal quantities option when editing products.
*/
function commerce_ss_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
if ($entity_form['#entity_type'] == 'commerce_product') {
// Get the product bundle type.
$type = $entity_form['#bundle'];
// Check if stock enabled for the type.
if (commerce_ss_product_type_enabled($type)) {
$decimal = variable_get('commerce_stock_decimal_backend_' . $type, FALSE);
if (!$decimal) {
// Get the fields language.
$language = $entity_form['commerce_stock']['#language'];
// Set the number format to decimal.
$entity_form['commerce_stock'][$language][0]['value']['#number_type'] = 'integer';
$entity_form['commerce_stock'][$language][0]['value']['#default_value'] = intval($entity_form['commerce_stock'][$language][0]['value']['#default_value']);
}
}
}
}
/**
* Determines whether stock management is enabled on a product type.
*
* @param string $type
* The product type.
*
* @return bool
* TRUE if stock management is enabled.
*/
function commerce_ss_product_type_enabled($type) {
$instance = field_info_instance('commerce_product', 'commerce_stock', $type);
return !empty($instance);
}
/**
* Determines whether stock management override is enabled on a product type.
*
* @param string $type
* The product type.
*
* @return bool
* TRUE if stock management override is enabled.
*/
function commerce_ss_product_type_override_enabled($type) {
$instance = field_info_instance('commerce_product', 'commerce_stock_override', $type);
return !empty($instance);
}
/**
* Determines whether stock management is enabled for a product.
*
* @param object $product
* The product to check.
*
* @return bool
* TRUE if stock management is enabled on the product's product type.
*/
function commerce_ss_product_enabled($product) {
return commerce_ss_product_type_enabled($product->type);
}
/**
* For a given a line item, determine whether stock management is enabled.
*
* @param array $line_item
* The line item to check.
*
* @return bool
* Boolean: TRUE if stock management is enabled on the product's product type.
*/
function commerce_ss_line_item_product_enabled($line_item) {
return commerce_ss_product_type_enabled($line_item->product->type);
}
/**
* This function makes sure stock is enabled for product.
*
* If true is return stock checking is enabled
* if false stock checking is disabled.
*
* @todo rename function confusing name it's not product that's not disabled
* its the stock check for the product that's disabled.
*
* @param mixed $product
* The product object.
*
* @return bool
* Return bool with flag: $stock_check_enabled.
*/
function commerce_ss_product_not_disabled_by_override($product) {
$stock_check_enabled = TRUE;
// If product overide enabled for the product type.
if (commerce_ss_product_type_override_enabled($product->type)) {
// If the product has its override set and is turned on.
if (isset($product->commerce_stock_override['und']) && $product->commerce_stock_override['und'][0]['value'] == 1) {
$stock_check_enabled = FALSE;
}
}
return $stock_check_enabled;
}
/**
* Implements hook_inline_entity_form_table_fields_alter().
*/
function commerce_ss_inline_entity_form_table_fields_alter(&$fields, $context) {
if ($context['entity_type'] == 'commerce_product') {
// Make sure there's a stock field on each of the allowed product types.
$has_stock_field = TRUE;
$decimal = FALSE;
foreach ($context['allowed_bundles'] as $bundle) {
if (!commerce_ss_product_type_enabled($bundle)) {
$has_stock_field = FALSE;
}
if (variable_get('commerce_stock_decimal_backend_' . $bundle, FALSE)) {
$decimal = TRUE;
}
}
if ($has_stock_field) {
$fields['commerce_stock'] = array(
'type' => 'field',
'label' => t('Stock'),
'formatter' => $decimal ? 'number_decimal' : 'number_integer',
'weight' => 101,
);
}
}
}