Skip to content

Commit

Permalink
Updating Commerce Stock to 7.x-2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
mwanberg committed Dec 4, 2018
1 parent 2749a53 commit b91dbd7
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 60 deletions.
23 changes: 23 additions & 0 deletions sites/all/modules/contrib/commerce_stock/commerce_stock.api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* @file
* Hooks provided by the Commerce Stock module.
*/


/**
* Allows modules to return a cart product level for a product id before the
* Commerce Stock module determines it using its default queries.
*
* @param $product_id
* The product_id of the product whose cart quantity should be returned.
*
* @return
* The cart quantity (if a valid quantity was found), FALSE (if the product
* should have no cart product level), or NULL (if the implementation cannot
* tell if the product has a cart product level or not).
*/
function hook_commerce_stock_cart_product_level_alter($product_id) {
// No example.
}
6 changes: 3 additions & 3 deletions sites/all/modules/contrib/commerce_stock/commerce_stock.info
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ dependencies[] = rules
core = 7.x
configure = admin/commerce/config/stock

; Information added by Drupal.org packaging script on 2014-12-11
version = "7.x-2.1"
; Information added by Drupal.org packaging script on 2016-03-29
version = "7.x-2.3"
core = "7.x"
project = "commerce_stock"
datestamp = "1418323685"
datestamp = "1459244643"

127 changes: 84 additions & 43 deletions sites/all/modules/contrib/commerce_stock/commerce_stock.module
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ function commerce_stock_permission() {
* Alters the add-to-cart form to show out-of-stock items and add a validator.
*/
function commerce_stock_form_alter(&$form, &$form_state, $form_id) {
$commerce_cart_form = 'commerce_cart_form';
if (module_exists('commerce_cart_view_override')) {
$commerce_cart_form = variable_get('commerce_cart_view_override_page_view', 'commerce_cart_form');
}

if (strpos($form_id, "commerce_cart_add_to_cart_form") === 0) {
// Check if product is disabled.
if (isset($form['submit']['#attributes']['disabled']) && ($form['submit']['#attributes']['disabled'] == 'disabled')) {
Expand All @@ -83,10 +88,25 @@ function commerce_stock_form_alter(&$form, &$form_state, $form_id) {
commerce_stock_cart_state_validate($form_id, $form, $form_state);
}
}
elseif ($form_id == 'views_form_commerce_cart_form_default') {
// Add validate function to the cart form.
$form['actions']['submit']['#validate'][] = 'commerce_stock_form_commerce_cart_validate';
$form['actions']['checkout']['#validate'][] = 'commerce_stock_form_commerce_cart_validate';
elseif (strpos($form_id, "views_form_{$commerce_cart_form}_") === 0) {
$view = reset($form_state['build_info']['args']);

// Only alter buttons if the cart form View shows line items.
if (!empty($view->result)) {
// Add validate function to the cart form.
if (empty($form['actions']['submit']['#validate'])) {
$form['actions']['submit']['#validate'] = array_merge($form['#validate'], array('commerce_stock_form_commerce_cart_validate'));
}
else {
$form['actions']['submit']['#validate'][] = 'commerce_stock_form_commerce_cart_validate';
}
if (empty($form['actions']['checkout']['#validate'])) {
$form['actions']['checkout']['#validate'] = array_merge($form['#validate'], array('commerce_stock_form_commerce_cart_validate'));
}
else {
$form['actions']['checkout']['#validate'][] = 'commerce_stock_form_commerce_cart_validate';
}
}
}
elseif ($form_id == 'commerce_checkout_form_checkout') {
// Add validate function to the checkout form.
Expand Down Expand Up @@ -197,21 +217,27 @@ function commerce_stock_form_commerce_cart_validate($form, &$form_state) {

$line_item_index = array_keys($form_state['line_items']);
if (isset($form_state['input']['edit_quantity'])) {
$products = array();
foreach ($form_state['values']['edit_quantity'] as $index => $qty) {
$line_item = $form_state['line_items'][$line_item_index[$index]];
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
if (in_array($line_item_wrapper->getBundle(), commerce_product_line_item_types())) {
$product_id = $line_item_wrapper->commerce_product->product_id->value();
$product = commerce_product_load($product_id);
// Check using rules.
commerce_stock_check_product_checkout_rule($product, $qty, $stock_state, $message);
// @todo: TEST and update error structure.
if ($stock_state == 1) {
form_set_error("stock_error", $message);
}
elseif ($stock_state == 2) {
drupal_set_message($message);
}
$products[$product_id]['qty'] = isset($products[$product_id]) ? $products[$product_id]['qty'] + $qty : $qty;
$products[$product_id]['line'] = $index;
}
}

foreach ($products as $product_id => $product) {
$prod = commerce_product_load($product_id);
// Check using rules.
commerce_stock_check_product_checkout_rule($prod, $product['qty'], $stock_state, $message);
// @todo: TEST and update error structure.
if ($stock_state == 1) {
form_set_error('edit_quantity][' . $product['line'], $message);
}
elseif ($stock_state == 2) {
drupal_set_message($message);
}
}
}
Expand Down Expand Up @@ -305,41 +331,59 @@ function commerce_stock_tokens($type, $tokens, array $data = array(), array $opt
}

/**
* Checks and returns quanity of the product and returns the value.
* Checks and returns quantity of the product and returns the value.
*
* The value is cached as is called more then once (including tokens)
*/
function commerce_stock_check_cart_product_level($product_id) {
// Cart product levels will be cached keyed by $product_id.
$cart_product_levels = &drupal_static(__FUNCTION__);

static $cart_product_level = array();
if (isset($cart_product_levels[$product_id])) {
return $cart_product_levels[$product_id];
}

// First let other modules attempt to provide a valid quantity for the given
// product id. Instead of invoking hook_commerce_stock_check_cart_product_level()
// directly, we invoke it in each module implementing the hook and return the
// first valid quantity returned (if any).
foreach (module_implements('commerce_stock_check_cart_product_level') as $module) {
$cart_qty = module_invoke($module, 'commerce_stock_check_cart_product_level', $product_id);

// If a hook said the product should not have a cart product level, that
// overrides any other potentially valid cart quantity. Return 0 now.
if ($cart_qty === FALSE) {
$cart_product_levels[$product_id] = 0;
return 0;
}

if (isset($cart_product_level[$product_id])) {
return $cart_product_level[$product_id];
// Otherwise only return a valid cart quantity.
if (!empty($cart_qty) && is_numeric($cart_qty)) {
$cart_product_levels[$product_id] = $cart_qty;
return $cart_qty;
}
}
else {

$cart_qty = 0;
global $user;
// Load the current cart if it exists.
$order = commerce_cart_order_load($user->uid);
if (!$order) {
$cart_qty = 0;
global $user;
// Load the current cart if it exists.
$order = commerce_cart_order_load($user->uid);
if (!$order) {
$cart_qty = 0;
}
else {
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
if ($order_wrapper) {
// Cycle throw each line item ID.
foreach ($order_wrapper->commerce_line_items as $index => $line_item_wrapper) {
if (in_array($line_item_wrapper->getBundle(), commerce_product_line_item_types())) {
if ($line_item_wrapper->commerce_product->product_id->value() == $product_id){
$cart_qty += $line_item_wrapper->quantity->value();
}
}
}
else {
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
// Cycle throw each line item ID.
foreach ($order_wrapper->commerce_line_items as $index => $line_item_wrapper) {
if (in_array($line_item_wrapper->getBundle(), commerce_product_line_item_types())) {
if ($line_item_wrapper->commerce_product->product_id->value() == $product_id){
$cart_qty += $line_item_wrapper->quantity->value();
}
}
}
$cart_product_level[$product_id] = $cart_qty;
return $cart_qty;
}
$cart_product_levels[$product_id] = $cart_qty;
return $cart_qty;
}

/**
Expand Down Expand Up @@ -384,12 +428,9 @@ function commerce_stock_check_product_checkout_rule($product, $qty_ordered, &$st
// Invoke the stock check event.
rules_invoke_event('commerce_stock_check_product_checkout', $product, $qty_ordered);

// If state not ok, do nothing then return the value set by the action.
if ($stock_check_data['state'] <> 0) {
$stock_state = $stock_check_data['state'];
$message = $stock_check_data['message'];
$qty = $stock_check_data['qty'];
}
// Set return values.
$stock_state = $stock_check_data['state'];
$message = $stock_check_data['message'];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ dependencies[] = commerce
dependencies[] = commerce_stock
core = 7.x
configure = admin/commerce/config/stock
; Information added by Drupal.org packaging script on 2014-12-11
version = "7.x-2.1"
; Information added by Drupal.org packaging script on 2016-03-29
version = "7.x-2.3"
core = "7.x"
project = "commerce_stock"
datestamp = "1418323685"
datestamp = "1459244643"

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ description = Provide a Decimal formater for converting stock levels into text m
package = Commerce (stock)
dependencies[] = number
core = 7.x
; Information added by Drupal.org packaging script on 2014-12-11
version = "7.x-2.1"
; Information added by Drupal.org packaging script on 2016-03-29
version = "7.x-2.3"
core = "7.x"
project = "commerce_stock"
datestamp = "1418323685"
datestamp = "1459244643"

Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ dependencies[] = commerce_order
dependencies[] = commerce_stock
dependencies[] = commerce_checkout
dependencies[] = rules
dependencies[] = list
core = 7.x
configure = admin/commerce/config/stock/ss
; Information added by Drupal.org packaging script on 2014-12-11
version = "7.x-2.1"
; Information added by Drupal.org packaging script on 2016-03-29
version = "7.x-2.3"
core = "7.x"
project = "commerce_stock"
datestamp = "1418323685"
datestamp = "1459244643"

Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function commerce_ss_entity_property_info_alter(&$info) {
}
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']);
}

}
Expand Down Expand Up @@ -150,6 +151,30 @@ function commerce_ss_form_commerce_product_ui_product_form_alter(&$form, &$form_
}
}

/**
* Implements hook_inline_entity_form_alter().
*
* Controll 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.
*
Expand Down Expand Up @@ -235,16 +260,21 @@ 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,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function commerce_ss_rules_action_info() {
function commerce_ss_decrease_by_line_item($line_item) {
if (in_array($line_item->type, commerce_product_line_item_types())) {
// The product SKU that will have its stock level adjusted.
$sku = $line_item->line_item_label;
$sku = commerce_ss_line_item_sku($line_item);
$product = commerce_product_load_by_sku($sku);
if (commerce_ss_product_type_enabled($product->type)) {
if (!(commerce_ss_product_type_override_enabled($product->type)
Expand All @@ -114,7 +114,7 @@ function commerce_ss_decrease_by_line_item($line_item) {
function commerce_ss_increase_by_line_item($line_item) {
if (in_array($line_item->type, commerce_product_line_item_types())) {
// The product SKU that will have its stock level adjusted.
$sku = $line_item->line_item_label;
$sku = commerce_ss_line_item_sku($line_item);
$product = commerce_product_load_by_sku($sku);
if (commerce_ss_product_type_enabled($product->type)) {
if (!(commerce_ss_product_type_override_enabled($product->type)
Expand Down Expand Up @@ -158,3 +158,14 @@ function commerce_ss_stock_adjust($product, $qty) {
watchdog('commerce_stock', 'Failed attempt to modify stock level of product %sku by %amount', array('%sku' => $product->sku, '%amount' => $qty), WATCHDOG_ERROR);
}
}

/**
* Helper function to get product SKU from line item.
*
* @param $line_item
* The cart line item for which to return a product SKU.
*/
function commerce_ss_line_item_sku($line_item) {
$line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
return($line_item_wrapper->commerce_product->sku->value());
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ dependencies[] = commerce_stock
dependencies[] = commerce_ss
dependencies[] = rules
core = 7.x
; Information added by Drupal.org packaging script on 2014-12-11
version = "7.x-2.1"
; Information added by Drupal.org packaging script on 2016-03-29
version = "7.x-2.3"
core = "7.x"
project = "commerce_stock"
datestamp = "1418323685"
datestamp = "1459244643"

0 comments on commit b91dbd7

Please sign in to comment.