diff --git a/uc_cart/tests/uc_cart.test b/uc_cart/tests/uc_cart.test
index fba5de1..bd5e689 100644
--- a/uc_cart/tests/uc_cart.test
+++ b/uc_cart/tests/uc_cart.test
@@ -114,6 +114,8 @@ class UbercartCartCheckoutTestCase extends UbercartTestHelper {
}
function testCart() {
+ module_enable(array('uc_cart_entity_test'));
+
// Test the empty cart.
$this->drupalGet('cart');
$this->assertText('There are no products in your shopping cart.');
@@ -121,6 +123,7 @@ class UbercartCartCheckoutTestCase extends UbercartTestHelper {
// Add an item to the cart.
$this->drupalPost('node/' . $this->product->nid, array(), t('Add to cart'));
$this->assertText($this->product->title . ' added to your shopping cart.');
+ $this->assertText('hook_uc_cart_item_insert fired');
// Test the cart page.
$this->drupalGet('cart');
@@ -130,6 +133,7 @@ class UbercartCartCheckoutTestCase extends UbercartTestHelper {
// Add the item again.
$this->drupalPost('node/' . $this->product->nid, array(), t('Add to cart'));
$this->assertText('Your item(s) have been updated.');
+ $this->assertText('hook_uc_cart_item_update fired');
// Test the cart page again.
$this->drupalGet('cart');
@@ -140,18 +144,20 @@ class UbercartCartCheckoutTestCase extends UbercartTestHelper {
$this->drupalPost('cart', array('items[0][qty]' => $qty), t('Update cart'));
$this->assertText('Your cart has been updated.');
$this->assertFieldByName('items[0][qty]', $qty, t('The product quantity was updated.'));
+ $this->assertText('hook_uc_cart_item_update fired');
// Update the quantity to zero.
$this->drupalPost('cart', array('items[0][qty]' => 0), t('Update cart'));
$this->assertText('Your cart has been updated.');
$this->assertText('There are no products in your shopping cart.');
+ $this->assertText('hook_uc_cart_item_delete fired');
// Test the remove item button.
$this->drupalPost('node/' . $this->product->nid, array(), t('Add to cart'));
$this->drupalPost('cart', array(), t('Remove'));
$this->assertText($this->product->title . ' removed from your shopping cart.');
$this->assertText('There are no products in your shopping cart.');
- $this->drupalLogout();
+ $this->assertText('hook_uc_cart_item_delete fired');
}
function testCartMerge() {
diff --git a/uc_cart/tests/uc_cart_entity_test.info b/uc_cart/tests/uc_cart_entity_test.info
new file mode 100644
index 0000000..9c20613
--- /dev/null
+++ b/uc_cart/tests/uc_cart_entity_test.info
@@ -0,0 +1,4 @@
+name = Cart item entity test module
+core = 7.x
+dependencies[] = uc_cart
+hidden = TRUE
diff --git a/uc_cart/tests/uc_cart_entity_test.module b/uc_cart/tests/uc_cart_entity_test.module
new file mode 100644
index 0000000..b536abd
--- /dev/null
+++ b/uc_cart/tests/uc_cart_entity_test.module
@@ -0,0 +1,22 @@
+qty < 1) {
+ if (isset($item->cart_item_id)) {
+ parent::delete(array($item->cart_item_id), $transaction);
+ }
+ }
+ else {
+ $item->changed = REQUEST_TIME;
+ parent::save($item, $transaction);
+ }
+ }
}
diff --git a/uc_cart/uc_cart.module b/uc_cart/uc_cart.module
index c840ff3..9eb2c44 100644
--- a/uc_cart/uc_cart.module
+++ b/uc_cart/uc_cart.module
@@ -250,9 +250,14 @@ function uc_cart_cron() {
*/
function uc_cart_node_delete($node) {
if (uc_product_is_product($node->type)) {
- db_delete('uc_cart_products')
- ->condition('nid', $node->nid)
+ $efq = new EntityFieldQuery();
+ $result = $efq->entityCondition('entity_type', 'uc_cart_item')
+ ->propertyCondition('nid', $node->nid)
->execute();
+
+ if (!empty($result['uc_cart_item'])) {
+ entity_delete_multiple('uc_cart_item', array_keys($result['uc_cart_item']));
+ }
}
}
@@ -1120,14 +1125,10 @@ function uc_cart_get_total_qty($cid = NULL) {
* unknown_type
*/
function uc_cart_update_item($item) {
- db_update('uc_cart_products')
- ->fields(array(
- 'qty' => $item->qty,
- 'changed' => REQUEST_TIME,
- 'data' => serialize($item->data),
- ))
- ->condition('cart_item_id', $item->cart_item_id)
- ->execute();
+ $item_entity = entity_load_single('uc_cart_item', $item->cart_item_id);
+ $item_entity->qty = $item->qty;
+ $item_entity->data = $item->data;
+ entity_save('uc_cart_item', $item_entity);
}
/**
@@ -1182,19 +1183,22 @@ function uc_cart_add_item($nid, $qty = 1, $data = NULL, $cid = NULL, $msg = TRUE
}
}
- $item = db_query("SELECT * FROM {uc_cart_products} WHERE cart_id = :cart_id AND nid = :nid AND data = :data", array(':cart_id' => $cid, ':nid' => $node->nid, ':data' => serialize($data)))->fetchObject();
+ $efq = new EntityFieldQuery();
+ $result = $efq->entityCondition('entity_type', 'uc_cart_item')
+ ->propertyCondition('cart_id', $cid)
+ ->propertyCondition('nid', $node->nid)
+ ->propertyCondition('data', serialize($data))
+ ->execute();
// If the item isn't in the cart yet, add it.
- if (is_null($item) || $item === FALSE) {
- db_insert('uc_cart_products')
- ->fields(array(
- 'cart_id' => $cid,
- 'nid' => $node->nid,
- 'qty' => $qty,
- 'changed' => REQUEST_TIME,
- 'data' => serialize($data),
- ))
- ->execute();
+ if (empty($result['uc_cart_item'])) {
+ $item_entity = entity_create('uc_cart_item', array(
+ 'cart_id' => $cid,
+ 'nid' => $node->nid,
+ 'qty' => $qty,
+ 'data' => $data,
+ ));
+ entity_save('uc_cart_item', $item_entity);
if ($msg) {
drupal_set_message(t('@product-title added to your shopping cart.', array('@product-title' => $node->title, '!url' => url('cart'))));
}
@@ -1204,7 +1208,8 @@ function uc_cart_add_item($nid, $qty = 1, $data = NULL, $cid = NULL, $msg = TRUE
if ($msg) {
drupal_set_message(t('Your item(s) have been updated.'));
}
- $qty += $item->qty;
+ $item_entity = entity_load_single('uc_cart_item', current(array_keys($result['uc_cart_item'])));
+ $qty += $item_entity->qty;
module_invoke($data['module'], 'uc_update_cart_item', $node->nid, $data, min($qty, 999999), $cid);
}
diff --git a/uc_product/uc_product.module b/uc_product/uc_product.module
index 6e7b657..991845c 100644
--- a/uc_product/uc_product.module
+++ b/uc_product/uc_product.module
@@ -1103,19 +1103,20 @@ function uc_product_uc_cart_display($item) {
function uc_product_uc_update_cart_item($nid, $data = array(), $qty, $cid = NULL) {
if (!$nid) return NULL;
$cid = !(is_null($cid) || empty($cid)) ? $cid : uc_cart_get_id();
- if ($qty < 1) {
- uc_cart_remove_item($nid, $cid, $data);
- }
- else {
- db_update('uc_cart_products')
- ->fields(array(
- 'qty' => $qty,
- 'changed' => REQUEST_TIME,
- ))
- ->condition('nid', $nid)
- ->condition('cart_id', $cid)
- ->condition('data', serialize($data))
- ->execute();
+
+ $efq = new EntityFieldQuery();
+ $result = $efq->entityCondition('entity_type', 'uc_cart_item')
+ ->propertyCondition('cart_id', $cid)
+ ->propertyCondition('nid', $nid)
+ ->propertyCondition('data', serialize($data))
+ ->execute();
+
+ if (!empty($result['uc_cart_item'])) {
+ $item_entity = entity_load_single('uc_cart_item', current(array_keys($result['uc_cart_item'])));
+ if ($item_entity->qty != $qty) {
+ $item_entity->qty = $qty;
+ entity_save('uc_cart_item', $item_entity);
+ }
}
}
diff --git a/uc_product_kit/uc_product_kit.module b/uc_product_kit/uc_product_kit.module
index 7ab112b..3e81f69 100644
--- a/uc_product_kit/uc_product_kit.module
+++ b/uc_product_kit/uc_product_kit.module
@@ -1146,21 +1146,15 @@ function uc_product_kit_uc_cart_display($item) {
function uc_product_kit_uc_update_cart_item($nid, $data = array(), $qty, $cid = NULL) {
if (!$nid) return NULL;
$cid = !(is_null($cid) || empty($cid)) ? $cid : uc_cart_get_id();
- if ($qty < 1) {
- foreach ($data as $p_nid => $product) {
- uc_cart_remove_item($p_nid, $cid, $product->data);
- }
+
+ if (isset($data['kit_id'])) {
+ // Product was listed individually.
+ uc_product_uc_update_cart_item($nid, $data, $qty, $cid);
}
else {
- if (isset($data['kit_id'])) {
- // Product was listed individually.
- uc_product_uc_update_cart_item($nid, $data, $qty, $cid);
- }
- else {
- $kit = node_load($nid);
- foreach ($data as $p_nid => $product) {
- uc_product_uc_update_cart_item($p_nid, $product->data, $qty * $kit->products[$p_nid]->qty, $cid);
- }
+ $kit = node_load($nid);
+ foreach ($data as $p_nid => $product) {
+ uc_product_uc_update_cart_item($p_nid, $product->data, $qty * $kit->products[$p_nid]->qty, $cid);
}
}
}