/** * Reads items from the request, then looks up the names from the lookup table * and returns a clean array of items ready for the database. * * @param array $items * @return array $cleanedItems */ protected function getCleanedEcommerceItems($items) { // Clean up the items array $cleanedItems = array(); foreach ($items as $item) { $name = $category = $category2 = $category3 = $category4 = $category5 = false; $price = 0; $quantity = 1; // items are passed in the request as an array: ( $sku, $name, $category, $price, $quantity ) if (empty($item[self::INDEX_ITEM_SKU])) { continue; } $sku = $item[self::INDEX_ITEM_SKU]; if (!empty($item[self::INDEX_ITEM_NAME])) { $name = $item[self::INDEX_ITEM_NAME]; } if (!empty($item[self::INDEX_ITEM_CATEGORY])) { $category = $item[self::INDEX_ITEM_CATEGORY]; } if (isset($item[self::INDEX_ITEM_PRICE]) && is_numeric($item[self::INDEX_ITEM_PRICE])) { $price = $this->getRevenue($item[self::INDEX_ITEM_PRICE]); } if (!empty($item[self::INDEX_ITEM_QUANTITY]) && is_numeric($item[self::INDEX_ITEM_QUANTITY])) { $quantity = (int) $item[self::INDEX_ITEM_QUANTITY]; } // self::INDEX_ITEM_* are in order $cleanedItems[] = array(self::INTERNAL_ITEM_SKU => $sku, self::INTERNAL_ITEM_NAME => $name, self::INTERNAL_ITEM_CATEGORY => $category, self::INTERNAL_ITEM_CATEGORY2 => $category2, self::INTERNAL_ITEM_CATEGORY3 => $category3, self::INTERNAL_ITEM_CATEGORY4 => $category4, self::INTERNAL_ITEM_CATEGORY5 => $category5, self::INTERNAL_ITEM_PRICE => $price, self::INTERNAL_ITEM_QUANTITY => $quantity); } // Lookup Item SKUs, Names & Categories Ids $actionsToLookupAllItems = array(); // Each item has 7 potential "ids" to lookup in the lookup table $columnsInEachRow = 1 + 1 + self::MAXIMUM_PRODUCT_CATEGORIES; foreach ($cleanedItems as $item) { $actionsToLookup = array(); list($sku, $name, $category, $price, $quantity) = $item; $actionsToLookup[] = array(trim($sku), Piwik_Tracker_Action::TYPE_ECOMMERCE_ITEM_SKU); $actionsToLookup[] = array(trim($name), Piwik_Tracker_Action::TYPE_ECOMMERCE_ITEM_NAME); // Only one category if (!is_array($category)) { $actionsToLookup[] = array(trim($category), Piwik_Tracker_Action::TYPE_ECOMMERCE_ITEM_CATEGORY); } else { $countCategories = 0; foreach ($category as $productCategory) { $productCategory = trim($productCategory); if (empty($productCategory)) { continue; } $countCategories++; if ($countCategories > self::MAXIMUM_PRODUCT_CATEGORIES) { break; } $actionsToLookup[] = array($productCategory, Piwik_Tracker_Action::TYPE_ECOMMERCE_ITEM_CATEGORY); } } // Ensure that each row has the same number of columns, fill in the blanks for ($i = count($actionsToLookup); $i < $columnsInEachRow; $i++) { $actionsToLookup[] = array(false, Piwik_Tracker_Action::TYPE_ECOMMERCE_ITEM_CATEGORY); } $actionsToLookupAllItems = array_merge($actionsToLookupAllItems, $actionsToLookup); } $actionsLookedUp = Piwik_Tracker_Action::loadActionId($actionsToLookupAllItems); // var_dump($actionsLookedUp); // Replace SKU, name & category by their ID action foreach ($cleanedItems as $index => &$item) { list($sku, $name, $category, $price, $quantity) = $item; // SKU $item[0] = $actionsLookedUp[$index * $columnsInEachRow + 0][2]; // Name $item[1] = $actionsLookedUp[$index * $columnsInEachRow + 1][2]; // Categories $item[2] = $actionsLookedUp[$index * $columnsInEachRow + 2][2]; $item[3] = $actionsLookedUp[$index * $columnsInEachRow + 3][2]; $item[4] = $actionsLookedUp[$index * $columnsInEachRow + 4][2]; $item[5] = $actionsLookedUp[$index * $columnsInEachRow + 5][2]; $item[6] = $actionsLookedUp[$index * $columnsInEachRow + 6][2]; } return $cleanedItems; }
/** * Reads items from the request, then looks up the names from the lookup table * and returns a clean array of items ready for the database. * * @param array $items * @return array $cleanedItems */ protected function getCleanedEcommerceItems($items) { // Clean up the items array $cleanedItems = array(); foreach($items as $item) { $name = $category = false; $price = 0; $quantity = 1; // items are passed in the request as an array: ( $sku, $name, $category, $price, $quantity ) if(empty($item[self::INDEX_ITEM_SKU])) { continue; } $sku = $item[self::INDEX_ITEM_SKU]; if(!empty($item[self::INDEX_ITEM_NAME])) { $name = $item[self::INDEX_ITEM_NAME]; } if(!empty($item[self::INDEX_ITEM_CATEGORY])) { $category = $item[self::INDEX_ITEM_CATEGORY]; } if(!empty($item[self::INDEX_ITEM_PRICE]) && is_numeric($item[self::INDEX_ITEM_PRICE])) { $price = $this->getRevenue($item[self::INDEX_ITEM_PRICE]); } if(!empty($item[self::INDEX_ITEM_QUANTITY]) && is_numeric($item[self::INDEX_ITEM_QUANTITY])) { $quantity = (int)$item[self::INDEX_ITEM_QUANTITY]; } // self::INDEX_ITEM_* are in order $cleanedItems[] = array( $sku, $name, $category, $price, $quantity ); } // Lookup Item SKUs, Names & Categories Ids $actionsToLookup = array(); foreach($cleanedItems as $item) { list($sku, $name, $category, $price, $quantity) = $item; $actionsToLookup[] = array($sku, Piwik_Tracker_Action::TYPE_ECOMMERCE_ITEM_SKU); $actionsToLookup[] = array($name, Piwik_Tracker_Action::TYPE_ECOMMERCE_ITEM_NAME); $actionsToLookup[] = array($category, Piwik_Tracker_Action::TYPE_ECOMMERCE_ITEM_CATEGORY); } $actionsLookedUp = Piwik_Tracker_Action::loadActionId($actionsToLookup); // var_dump($actionsLookedUp); // Replace SKU, name & category by their ID action foreach($cleanedItems as $index => &$item) { list($sku, $name, $category, $price, $quantity) = $item; // SKU $item[0] = $actionsLookedUp[ $index * 3 + self::INDEX_ITEM_SKU][2]; // Name $item[1] = $actionsLookedUp[ $index * 3 + self::INDEX_ITEM_NAME][2]; // Category $item[2] = $actionsLookedUp[ $index * 3 + self::INDEX_ITEM_CATEGORY][2]; } return $cleanedItems; }