protected function recordEcommerceItems($goal, $items)
{
$itemInCartBySku = array();
foreach ($items as $item) {
$itemInCartBySku[$item[0]] = $item;
}
$itemsInDb = $this->getModel()->getAllItemsCurrentlyInTheCart($goal, self::ITEM_IDORDER_ABANDONED_CART);
// Look at which items need to be deleted, which need to be added or updated, based on the SKU
$skuFoundInDb = $itemsToUpdate = array();
foreach ($itemsInDb as $itemInDb) {
$skuFoundInDb[] = $itemInDb['idaction_sku'];
// Ensure price comparisons will have the same assumption
$itemInDb['price'] = $this->getRevenue($itemInDb['price']);
$itemInDbOriginal = $itemInDb;
$itemInDb = array_values($itemInDb);
// Cast all as string, because what comes out of the fetchAll() are strings
$itemInDb = $this->getItemRowCast($itemInDb);
//Item in the cart in the DB, but not anymore in the cart
if (!isset($itemInCartBySku[$itemInDb[0]])) {
$itemToUpdate = array_merge($itemInDb, array('deleted' => 1, 'idorder_original_value' => $itemInDbOriginal['idorder_original_value']));
$itemsToUpdate[] = $itemToUpdate;
Common::printDebug("Item found in the previous Cart, but no in the current cart/order");
Common::printDebug($itemToUpdate);
continue;
}
$newItem = $itemInCartBySku[$itemInDb[0]];
$newItem = $this->getItemRowCast($newItem);
if (count($itemInDb) != count($newItem)) {
Common::printDebug("ERROR: Different format in items from cart and DB");
throw new Exception(" Item in DB and Item in cart have a different format, this is not expected... " . var_export($itemInDb, true) . var_export($newItem, true));
}
Common::printDebug("Item has changed since the last cart. Previous item stored in cart in database:");
Common::printDebug($itemInDb);
Common::printDebug("New item to UPDATE the previous row:");
$newItem['idorder_original_value'] = $itemInDbOriginal['idorder_original_value'];
Common::printDebug($newItem);
$itemsToUpdate[] = $newItem;
}
// Items to UPDATE
$this->updateEcommerceItems($goal, $itemsToUpdate);
// Items to INSERT
$itemsToInsert = array();
foreach ($items as $item) {
if (!in_array($item[0], $skuFoundInDb)) {
$itemsToInsert[] = $item;
}
}
$this->insertEcommerceItems($goal, $itemsToInsert);
}