| 931 |
function uc_product_kit_add_to_cart($nid, $qty, $kit_data) { |
function uc_product_kit_add_to_cart($nid, $qty, $kit_data) { |
| 932 |
$node = node_load($nid); |
$node = node_load($nid); |
| 933 |
if ($node->type == 'product_kit') { |
if ($node->type == 'product_kit') { |
| 934 |
|
$cart = uc_cart_get_contents(); |
| 935 |
$unique = uniqid('', TRUE); |
$unique = uniqid('', TRUE); |
| 936 |
|
$update = array(); |
| 937 |
|
$product_data = array(); |
| 938 |
|
|
| 939 |
foreach ($node->products as $product) { |
foreach ($node->products as $product) { |
| 940 |
$data = array('kit_id' => $node->nid, 'unique_id' => $unique, 'module' => 'uc_product_kit'); |
$data = array('kit_id' => $node->nid, 'module' => 'uc_product_kit') + module_invoke_all('add_to_cart_data', $kit_data['products'][$product->nid]); |
| 941 |
uc_cart_add_item($product->nid, $product->qty * $qty, $data + module_invoke_all('add_to_cart_data', $kit_data['products'][$product->nid]), NULL, FALSE, FALSE, FALSE); |
$product_data[$product->nid] = $data; |
| 942 |
|
foreach ($cart as $item) { |
| 943 |
|
if ($item->nid == $product->nid && isset($item->data['kit_id']) && $item->data['kit_id'] == $node->nid) { |
| 944 |
|
// There is something in the cart like the product kit. Update |
| 945 |
|
// by default, but check that it's possible. |
| 946 |
|
if (is_null($add_new)) { |
| 947 |
|
$add_new = FALSE; |
| 948 |
|
} |
| 949 |
|
$data['unique_id'] = $item->data['unique_id']; |
| 950 |
|
if ($item->data == $data) { |
| 951 |
|
// This product is a candidate for updating the cart quantity. |
| 952 |
|
// Make sure the data arrays will compare as equal when serialized. |
| 953 |
|
$product_data[$product->nid] = $item->data; |
| 954 |
|
$update[$product->nid] = TRUE; |
| 955 |
|
} |
| 956 |
|
} |
| 957 |
|
} |
| 958 |
|
|
| 959 |
|
} |
| 960 |
|
|
| 961 |
|
// The product kit can update its items only if they all can be updated. |
| 962 |
|
if (count($update) != count($node->products)) { |
| 963 |
|
foreach ($node->products as $product) { |
| 964 |
|
$data = $product_data[$product->nid]; |
| 965 |
|
$data['unique_id'] = $unique; |
| 966 |
|
uc_cart_add_item($product->nid, $product->qty * $qty, $data, NULL, FALSE, FALSE); |
| 967 |
|
} |
| 968 |
|
} |
| 969 |
|
else { |
| 970 |
|
foreach ($node->products as $product) { |
| 971 |
|
$data = $product_data[$product->nid]; |
| 972 |
|
uc_cart_add_item($product->nid, $product->qty * $qty, $data, NULL, FALSE, FALSE); |
| 973 |
|
} |
| 974 |
} |
} |
| 975 |
|
|
| 976 |
// Rebuild the cart items cache. |
// Rebuild the cart items cache. |