diff --git a/uc_stock/uc_stock.module b/uc_stock/uc_stock.module index f4d12be..2166325 100644 --- a/uc_stock/uc_stock.module +++ b/uc_stock/uc_stock.module @@ -311,28 +311,76 @@ function uc_stock_increment_product_stock($product, $key, $order) { * @param $order * Order object. */ -function uc_stock_adjust_product_stock($product, $key, $order) { - // Product has an active stock? - if (!uc_stock_is_active($product->model)) { - return; - } + function uc_stock_adjust_product_stock($product, $key, $order) { + + $product_sku = get_attribute_sku($product); + + // Product has an active stock? + if (!uc_stock_is_active($product_sku)) { + return; + } + + // Do nothing if decrement quantity is 0 + if ($product->qty == 0) { + return; + } + + // Adjust the product's stock. + uc_stock_adjust($product_sku, -$product->qty); - // Do nothing if decrement quantity is 0 - if ($product->qty == 0) { - return; + // Load the new stock record + $stock = db_fetch_object(db_query("SELECT * FROM {uc_product_stock} WHERE sku = '%s'", $product_sku)); + + // Should we notify? + if (variable_get('uc_stock_threshold_notification', FALSE) && $stock->stock <= $stock->threshold) { + _uc_stock_send_mail($order, $stock); + } + + // Save a comment about the stock level. + uc_order_comment_save($order->order_id, 0, t('The stock level for %model_name has been !action to !qty.', array('%model_name' => $product_sku, '!qty' => $stock->stock, '!action' => (-$product->qty <= 0) ? t('decreased') : t('increased') ))); } - // Adjust the product's stock. - uc_stock_adjust($product->model, -$product->qty); + function get_attribute_sku($product) { + if (!isset($product->data['attributes']) + || !is_array($product->data['attributes']) + || empty($product->data['attributes'])) { + return $product->model; + } + + $sql = "select model,combination from {uc_product_adjustments} where nid = %d"; + $results = db_query($sql,$product->nid); - // Load the new stock record - $stock = db_fetch_object(db_query("SELECT * FROM {uc_product_stock} WHERE sku = '%s'", $product->model)); + ksort($product->data['attributes']); + $product_attributes = convert_product_attributes_for_comparison($product); - // Should we notify? - if (variable_get('uc_stock_threshold_notification', FALSE) && $stock->stock <= $stock->threshold) { - _uc_stock_send_mail($order, $stock); + while($result = db_fetch_object($results)) { + $candidateAttributes = unserialize($result->combination); + ksort($candidateAttributes); + if ($product_attributes == $candidateAttributes) { + return $result->model; + } + } + //if there is no attribute match use the product model + return $product->model; } - // Save a comment about the stock level. - uc_order_comment_save($order->order_id, 0, t('The stock level for %model_name has been !action to !qty.', array('%model_name' => $product->model, '!qty' => $stock->stock, '!action' => (-$product->qty <= 0) ? t('decreased') : t('increased') ))); -} + /** + * In order to compare the product attributes from uc_cart uc_cart_get_item with those + * unserialized from uc_product_adjustments.combination the product attributes array must be + * converted to a comparable structure + * @param $product + * the product loaded by uc_cart + * @return array + * an array with numeric aid as key and oid as value for each attribute in the product + */ + function convert_product_attributes_for_comparison($product) + { + $attributes = array(); + $attribute_name_to_id_sql = "select aid from uc_attributes where name = '%s'"; + foreach ($product->data['attributes'] as $attribute_name => $oid) { + $attribute_id = db_result(db_query($attribute_name_to_id_sql, $attribute_name)); + $attributes[$attribute_id] = key($oid); + } + ksort($attributes); + return $attributes; + }