From b91dbd7b589113a2dc895489e6c95781a0285a99 Mon Sep 17 00:00:00 2001 From: Marlene Williams Date: Mon, 3 Dec 2018 19:39:51 -0800 Subject: [PATCH] Updating Commerce Stock to 7.x-2.3 --- .../commerce_stock/commerce_stock.api.php | 23 ++++ .../commerce_stock/commerce_stock.info | 6 +- .../commerce_stock/commerce_stock.module | 127 ++++++++++++------ .../commerce_stock/commerce_stock_ui.info | 6 +- .../modules/commerce_sdf/commerce_sdf.info | 6 +- .../modules/commerce_ss/commerce_ss.info | 7 +- .../modules/commerce_ss/commerce_ss.module | 30 +++++ .../modules/commerce_ss/commerce_ss.rules.inc | 15 ++- .../modules/commerce_ssr/commerce_ssr.info | 6 +- 9 files changed, 166 insertions(+), 60 deletions(-) create mode 100644 sites/all/modules/contrib/commerce_stock/commerce_stock.api.php diff --git a/sites/all/modules/contrib/commerce_stock/commerce_stock.api.php b/sites/all/modules/contrib/commerce_stock/commerce_stock.api.php new file mode 100644 index 00000000..e0e70f32 --- /dev/null +++ b/sites/all/modules/contrib/commerce_stock/commerce_stock.api.php @@ -0,0 +1,23 @@ +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. @@ -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); } } } @@ -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; } /** @@ -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']; } /** diff --git a/sites/all/modules/contrib/commerce_stock/commerce_stock_ui.info b/sites/all/modules/contrib/commerce_stock/commerce_stock_ui.info index 4e0f513d..632479d5 100644 --- a/sites/all/modules/contrib/commerce_stock/commerce_stock_ui.info +++ b/sites/all/modules/contrib/commerce_stock/commerce_stock_ui.info @@ -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" diff --git a/sites/all/modules/contrib/commerce_stock/modules/commerce_sdf/commerce_sdf.info b/sites/all/modules/contrib/commerce_stock/modules/commerce_sdf/commerce_sdf.info index 117b9214..913c5975 100644 --- a/sites/all/modules/contrib/commerce_stock/modules/commerce_sdf/commerce_sdf.info +++ b/sites/all/modules/contrib/commerce_stock/modules/commerce_sdf/commerce_sdf.info @@ -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" diff --git a/sites/all/modules/contrib/commerce_stock/modules/commerce_ss/commerce_ss.info b/sites/all/modules/contrib/commerce_stock/modules/commerce_ss/commerce_ss.info index 35262f7a..e2c6414a 100644 --- a/sites/all/modules/contrib/commerce_stock/modules/commerce_ss/commerce_ss.info +++ b/sites/all/modules/contrib/commerce_stock/modules/commerce_ss/commerce_ss.info @@ -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" diff --git a/sites/all/modules/contrib/commerce_stock/modules/commerce_ss/commerce_ss.module b/sites/all/modules/contrib/commerce_stock/modules/commerce_ss/commerce_ss.module index af22fd0a..03b53ea3 100644 --- a/sites/all/modules/contrib/commerce_stock/modules/commerce_ss/commerce_ss.module +++ b/sites/all/modules/contrib/commerce_stock/modules/commerce_ss/commerce_ss.module @@ -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']); } } @@ -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. * @@ -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, ); } diff --git a/sites/all/modules/contrib/commerce_stock/modules/commerce_ss/commerce_ss.rules.inc b/sites/all/modules/contrib/commerce_stock/modules/commerce_ss/commerce_ss.rules.inc index ac39a154..122c2264 100644 --- a/sites/all/modules/contrib/commerce_stock/modules/commerce_ss/commerce_ss.rules.inc +++ b/sites/all/modules/contrib/commerce_stock/modules/commerce_ss/commerce_ss.rules.inc @@ -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) @@ -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) @@ -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()); +} diff --git a/sites/all/modules/contrib/commerce_stock/modules/commerce_ssr/commerce_ssr.info b/sites/all/modules/contrib/commerce_stock/modules/commerce_ssr/commerce_ssr.info index 06979543..d1dee1ca 100644 --- a/sites/all/modules/contrib/commerce_stock/modules/commerce_ssr/commerce_ssr.info +++ b/sites/all/modules/contrib/commerce_stock/modules/commerce_ssr/commerce_ssr.info @@ -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"