diff --git a/uc_cart/uc_cart.module b/uc_cart/uc_cart.module index e38add3..ae503e0 100644 --- a/uc_cart/uc_cart.module +++ b/uc_cart/uc_cart.module @@ -1423,6 +1423,16 @@ function uc_cart_get_item($item) { return; } + $item->data = unserialize($item->data); + uc_cart_prepare_item($item); + + return $item; +} + +/** + * Prepare a cart item object. + */ +function uc_cart_prepare_item($item) { $product = node_load($item->nid); if (!$product) { return; @@ -1433,20 +1443,14 @@ function uc_cart_get_item($item) { $item->cost = $product->cost; $item->price = $product->sell_price; $item->weight = $product->weight; - $item->data = unserialize($item->data); + $item->weight_units = $product->weight_units; $item->module = $item->data['module']; $item->model = $product->model; - // Invoke hook_cart_item() with $op = 'load' in enabled modules. - foreach (module_list() as $module) { - $func = $module .'_cart_item'; - if (function_exists($func)) { - // $item must be passed by reference. - $func('load', $item); - } + foreach (module_implements('cart_item') as $module) { + $func = $module . '_cart_item'; + $func('load', $item); } - - return $item; } /** @@ -1519,6 +1523,27 @@ function uc_cart_add_item($nid, $qty = 1, $data = NULL, $cid = NULL, $msg = TRUE } } + // We need the full cart item including SKU to discover shippability. + $item = new stdClass; + $item->nid = $nid; + $item->qty = $qty; + $item->data = $data; + uc_cart_prepare_item($item); + $data = $item->data; + + $results = array(); + foreach (module_implements('cart_item') as $module) { + $func = $module .'_cart_item'; + $return = $func('can_ship', $item); + if (!is_null($return)) { + $results[] = $return; + } + } + + // The product is shippable if any module says that it is shippable, + // or if no module returned a result and the product node is shippable. + $data['shippable'] = !empty($results) ? in_array(TRUE, $results) : $node->shippable; + $item = db_fetch_object(db_query("SELECT * FROM {uc_cart_products} WHERE cart_id = '%s' AND nid = %d AND data = '%s'", $cid, $node->nid, serialize($data))); // If the item isn't in the cart yet, add it. @@ -1680,35 +1705,10 @@ function uc_cart_is_shippable($cart_id = NULL) { } /** - * Determines whether a product is shippable or not. + * Determines whether an item is shippable or not. */ -function uc_cart_product_is_shippable($product) { - // Return FALSE if the product form specifies this as not shippable. - if ($product->data['shippable'] == FALSE) { - return FALSE; - } - - $result = array(); - - // See if any other modules have a say in the matter... - foreach (module_list() as $module) { - $func = $module .'_cart_item'; - if (function_exists($func)) { - // $product must be passed by reference. - $return = $func('can_ship', $product); - - if (!is_null($return)) { - $result[] = $return; - } - } - } - - // Return TRUE by default. - if (empty($result) || in_array(TRUE, $result)) { - return TRUE; - } - - return FALSE; +function uc_cart_product_is_shippable($item) { + return !empty($item->data['shippable']); } /** diff --git a/uc_order/uc_order.admin.inc b/uc_order/uc_order.admin.inc index 951aa9c..065f3e2 100644 --- a/uc_order/uc_order.admin.inc +++ b/uc_order/uc_order.admin.inc @@ -1315,17 +1315,27 @@ function uc_order_edit_products($order) { break; case 'add': $form_state['values'] = $_POST; - $product = node_load(intval($_POST['nid'])); + $product = new stdClass; + $product->nid = intval($_POST['nid']); $product->qty = intval($_POST['qty']); - $product->price = $product->sell_price; $product->data = module_invoke_all('add_to_cart_data', $form_state['values']); - foreach (module_list() as $module) { - $function = $module .'_cart_item'; - if (function_exists($function)) { - // $product must be passed by reference. - $function('load', $product); + uc_cart_prepare_item($product); + + if(!isset($product->data['shippable'])) { + $node = node_load(intval($_POST['nid'])); + $results = array(); + foreach (module_implements('cart_item') as $module) { + $func = $module .'_cart_item'; + $return = $func('can_ship', $product); + if (!is_null($return)) { + $results[] = $return; + } } + + // The product is shippable if any module says that it is shippable, + // or if no module returned a result and the product node is shippable. + $product->data['shippable'] = !empty($results) ? in_array(TRUE, $results) : $node->shippable; } $price_info = array( diff --git a/uc_order/uc_order.module b/uc_order/uc_order.module index 6077c61..8d0345d 100644 --- a/uc_order/uc_order.module +++ b/uc_order/uc_order.module @@ -1592,33 +1592,12 @@ function uc_order_is_shippable($order) { } foreach ($order->products as $product) { - // Return FALSE if the product form specifies this as not shippable. - if (empty($product->data['shippable'])) { - $results[] = FALSE; - continue; - } - - // See if any other modules have a say in the matter... - foreach (module_list() as $module) { - $function = $module .'_cart_item'; - if (function_exists($function)) { - // $product must be passed by reference to hook_cart_item. - $can_ship = $function('can_ship', $product); - if (!is_null($can_ship)) { - $result[] = $can_ship; - } - } - } - - // Return TRUE by default. - if (empty($result) || in_array(TRUE, $result)) { - $results[] = TRUE; - continue; + if (!empty($product->data['shippable'])) { + return TRUE; } - $results[] = FALSE; } - return in_array(TRUE, $results); + return FALSE; } /** diff --git a/uc_product/uc_product.module b/uc_product/uc_product.module index dcc3496..d4c37ed 100644 --- a/uc_product/uc_product.module +++ b/uc_product/uc_product.module @@ -1283,14 +1283,6 @@ function uc_product_update_cart_item($nid, $data = array(), $qty, $cid = NULL) { } /** - * Implements hook_add_to_cart_data(). - */ -function uc_product_add_to_cart_data($form_values) { - $node = node_load($form_values['nid']); - return array('shippable' => $node->shippable); -} - -/** * Implements hook_product_class(). */ function uc_product_product_class($pcid, $op) {