public static function storeProduct($data, $importColumn = 1)
{
// Store
$table = Table::getInstance('PhocaCartItem', 'Table', array());
$newInsertOldId = 0;
if ($importColumn == 2) {
// SKU
if (isset($data['sku']) && $data['sku'] != '') {
$found = $table->load(array('sku' => $data['sku']));
// Such id is found, but we store by SKU - we need to unset it to get new created by autoincrement
if ($found) {
$data['id'] = $table->id;
} else {
// New row
//unset($data['id']); store the same ID for importing product if possible
// unfortunately this is not possible per standard way
// We didn't find the row by SKU, but we have the ID, so we try to update by ID
// If we don't find the ID (so no SKU, no ID), insert new row
// We try to add current ID (it does not exist), not new autoincrement
$found2 = $table->load((int) $data['id']);
if (!$found2) {
$newInsertOldId = 1;
}
}
}
} else {
// ID
if (isset($data['id']) && (int) $data['id'] > 0) {
$found = $table->load((int) $data['id']);
// Such id not found, we need to unset it to get new created by autoincrement
if (!$found) {
// New row
//unset($data['id']); store the same ID for importing product if possible
// unfortunately this is not possible per standard way
$newInsertOldId = 1;
}
}
}
if (!$table->bind($data)) {
throw new Exception($table->getError());
return false;
}
if (intval($table->date) == 0) {
$table->date = Factory::getDate()->toSql();
}
if (!$table->check()) {
throw new Exception($table->getError());
return false;
}
if ($newInsertOldId == 1) {
// The imported ID does not exist, we need to add new row, but we try to use the same ID
// even the ID is autoincrement (this is why we use non standard method) because
// standard method cannot add IDs into autoincrement
$db = Factory::getDBO();
if (!$db->insertObject('#__phocacart_products', $table, 'id')) {
throw new Exception($table->getError());
return false;
}
} else {
if (!$table->store()) {
throw new Exception($table->getError());
return false;
}
}
// Test Thumbnails (Create if not exists)
if ($table->image != '') {
$thumb = PhocacartFileThumbnail::getOrCreateThumbnail($table->image, '', 1, 1, 1, 0, 'productimage');
}
if ((int) $table->id > 0) {
if (!isset($data['catid_multiple'])) {
$data['catid_multiple'] = array();
}
if (!isset($data['catid_multiple_ordering'])) {
$data['catid_multiple_ordering'] = array();
}
PhocacartCategoryMultiple::storeCategories($data['catid_multiple'], (int) $table->id, $data['catid_multiple_ordering']);
if (isset($data['featured'])) {
PhocacartProduct::featured((int) $table->id, $data['featured']);
}
$dataRelated = '';
if (!isset($data['related'])) {
$dataRelated = '';
} else {
$dataRelated = $data['related'];
if (is_array($data['related']) && isset($data['related'][0])) {
$dataRelated = $data['related'][0];
}
}
$advancedStockOptions = '';
if (!isset($data['advanced_stock_options'])) {
$advancedStockOptions = '';
} else {
$advancedStockOptions = $data['advanced_stock_options'];
if (is_array($data['advanced_stock_options']) && isset($data['advanced_stock_options'][0])) {
$advancedStockOptions = $data['advanced_stock_options'][0];
}
}
$additionalDownloadFiles = '';
if (!isset($data['additional_download_files'])) {
$additionalDownloadFiles = '';
} else {
$additionalDownloadFiles = $data['additional_download_files'];
}
PhocacartRelated::storeRelatedItemsById($dataRelated, (int) $table->id);
PhocacartImageAdditional::storeImagesByProductId((int) $table->id, $data['images']);
PhocacartAttribute::storeAttributesById((int) $table->id, $data['attributes']);
PhocacartAttribute::storeCombinationsById((int) $table->id, $advancedStockOptions);
PhocacartSpecification::storeSpecificationsById((int) $table->id, $data['specifications']);
PhocacartDiscountProduct::storeDiscountsById((int) $table->id, $data['discounts']);
PhocacartFileAdditional::storeProductFilesByProductId((int) $table->id, $additionalDownloadFiles);
PhocacartTag::storeTags($data['tags'], (int) $table->id);
PhocacartTag::storeTagLabels($data['taglabels'], (int) $table->id);
// PARAMETERS
$parameters = PhocacartParameter::getAllParameters();
if (!empty($parameters)) {
foreach ($parameters as $kP => $vP) {
if (isset($vP->id) && (int) $vP->id > 0) {
$idP = (int) $vP->id;
if (!empty($data['items_parameter'][$idP])) {
PhocacartParameter::storeParameterValues($data['items_parameter'][$idP], (int) $table->id, $idP);
} else {
PhocacartParameter::storeParameterValues(array(), (int) $table->id, $idP);
}
}
}
}
PhocacartGroup::storeProductPriceGroupsById($data['price_groups'], (int) $table->id);
PhocacartGroup::storeProductPointGroupsById($data['point_groups'], (int) $table->id);
PhocacartGroup::storeGroupsById((int) $table->id, 3, $data['groups']);
PhocacartPriceHistory::storePriceHistoryCustomById($data['price_histories'], (int) $table->id);
PhocacartGroup::updateGroupProductPriceById((int) $table->id, $data['price']);
PhocacartGroup::updateGroupProductRewardPointsById((int) $table->id, $data['points_received']);
return $table->id;
}
return false;
}