Check if id_category_default is set, on all lines
This commit is contained in:
parent
7da68f2768
commit
bd363dbd21
@ -216,95 +216,120 @@ class AdminBulkUpdate extends AdminTab {
|
||||
fclose($f);
|
||||
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
|
||||
}
|
||||
} elseif(Tools::isSubmit('submitUploadCategories')) {
|
||||
}
|
||||
// Update product categories and default category
|
||||
elseif (Tools::isSubmit('submitUploadCategories')) {
|
||||
|
||||
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
|
||||
fgetcsv($f, 0, ';');
|
||||
|
||||
$products = array();
|
||||
$defaults = array();
|
||||
$positions = array();
|
||||
|
||||
while($line = fgetcsv($f, 0, ';')) {
|
||||
if(empty($line[0])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!isset($line[1])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(isset($line[2]) && !empty($line[2]) && (int) $line[2] != 0) {
|
||||
$defaults[(int) $line[0]] = (int) $line[2];
|
||||
}
|
||||
|
||||
$categories = array_map('intval', explode(',', $line[1]));
|
||||
$products[(int) $line[0]] = $categories;
|
||||
foreach($categories as $id_category) {
|
||||
$positions[(int) $id_category] = 1;
|
||||
// Check
|
||||
$error = false;
|
||||
while ($line = fgetcsv($f, 0, ';')) {
|
||||
// Check id_category_default
|
||||
if (empty($line[2])) {
|
||||
$error = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fclose($f);
|
||||
|
||||
if(Tools::getValue('category_process') == 1) {
|
||||
Db::getInstance()->ExecuteS('
|
||||
DELETE FROM `'._DB_PREFIX_.'category_product`
|
||||
WHERE `id_product` IN ('.implode(', ', array_keys($products)).')
|
||||
');
|
||||
|
||||
if ($error == true) {
|
||||
$output .= '<p class="warn">'.$this->l('ID Category default not set, check your file').'</p>';
|
||||
}
|
||||
|
||||
foreach(Db::getInstance()->ExecuteS('
|
||||
SELECT `id_category`, MAX(`position`) AS `position`
|
||||
FROM `'._DB_PREFIX_.'category_product`
|
||||
WHERE `id_category` IN ('.implode(', ', array_keys($positions)).')
|
||||
GROUP BY `id_category`
|
||||
') as $row) {
|
||||
$positions[(int) $row['id_category']] = (int) $row['position'];
|
||||
}
|
||||
|
||||
foreach($products as $id_product => $id_categories) {
|
||||
foreach($id_categories as $id_category) {
|
||||
// Import
|
||||
else {
|
||||
$products = array();
|
||||
$defaults = array();
|
||||
$positions = array();
|
||||
|
||||
rewind($f);
|
||||
|
||||
while ($line = fgetcsv($f, 0, ';')) {
|
||||
// id_product
|
||||
if (empty($line[0])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// id_category
|
||||
if (!isset($line[1])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// id_category_default
|
||||
if (isset($line[2]) && !empty($line[2]) && (int) $line[2] != 0) {
|
||||
$defaults[(int) $line[0]] = (int) $line[2];
|
||||
}
|
||||
|
||||
$categories = array_map('intval', explode(',', $line[1]));
|
||||
$products[(int) $line[0]] = $categories;
|
||||
foreach($categories as $id_category) {
|
||||
$positions[(int) $id_category] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
fclose($f);
|
||||
|
||||
if(Tools::getValue('category_process') == 1) {
|
||||
Db::getInstance()->ExecuteS('
|
||||
DELETE FROM `'._DB_PREFIX_.'category_product`
|
||||
WHERE `id_product` IN ('.implode(', ', array_keys($products)).')
|
||||
');
|
||||
}
|
||||
|
||||
foreach(Db::getInstance()->ExecuteS('
|
||||
SELECT `id_category`, MAX(`position`) AS `position`
|
||||
FROM `'._DB_PREFIX_.'category_product`
|
||||
WHERE `id_category` IN ('.implode(', ', array_keys($positions)).')
|
||||
GROUP BY `id_category`
|
||||
') as $row) {
|
||||
$positions[(int) $row['id_category']] = (int) $row['position'];
|
||||
}
|
||||
|
||||
foreach($products as $id_product => $id_categories) {
|
||||
foreach($id_categories as $id_category) {
|
||||
Db::getInstance()->ExecuteS('
|
||||
INSERT INTO `'._DB_PREFIX_.'category_product` VALUES (
|
||||
'.(int) $id_category.',
|
||||
'.(int) $id_product.',
|
||||
'.(int) ++$positions[(int) $id_category].'
|
||||
)
|
||||
');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach($defaults as $id_product => $id_category_default) {
|
||||
Db::getInstance()->ExecuteS('
|
||||
UPDATE `'._DB_PREFIX_.'product`
|
||||
SET `id_category_default` = '.(int) $id_category_default.'
|
||||
WHERE `id_product` = '.(int) $id_product.'
|
||||
LIMIT 1
|
||||
');
|
||||
|
||||
if(!$row = Db::getInstance()->getRow('
|
||||
SELECT `id_product`
|
||||
FROM `'._DB_PREFIX_.'category_product`
|
||||
WHERE `id_product` = '.(int) $id_product.'
|
||||
AND `id_category` = '.(int) $id_category_default.'
|
||||
')) {
|
||||
$pos = (int) Db::getInstance()->getValue('
|
||||
SELECT MAX(`position`)
|
||||
FROM `'._DB_PREFIX_.'category_product`
|
||||
WHERE `id_category` = '.(int) $id_category_default.'
|
||||
') + 1;
|
||||
Db::getInstance()->ExecuteS('
|
||||
INSERT INTO `'._DB_PREFIX_.'category_product` VALUES (
|
||||
'.(int) $id_category_default.',
|
||||
'.(int) $id_product.',
|
||||
'.(int) $pos.'
|
||||
)
|
||||
|
||||
foreach($defaults as $id_product => $id_category_default) {
|
||||
Db::getInstance()->ExecuteS('
|
||||
UPDATE `'._DB_PREFIX_.'product`
|
||||
SET `id_category_default` = '.(int) $id_category_default.'
|
||||
WHERE `id_product` = '.(int) $id_product.'
|
||||
LIMIT 1
|
||||
');
|
||||
|
||||
if(!$row = Db::getInstance()->getRow('
|
||||
SELECT `id_product`
|
||||
FROM `'._DB_PREFIX_.'category_product`
|
||||
WHERE `id_product` = '.(int) $id_product.'
|
||||
AND `id_category` = '.(int) $id_category_default.'
|
||||
')) {
|
||||
$pos = (int) Db::getInstance()->getValue('
|
||||
SELECT MAX(`position`)
|
||||
FROM `'._DB_PREFIX_.'category_product`
|
||||
WHERE `id_category` = '.(int) $id_category_default.'
|
||||
') + 1;
|
||||
Db::getInstance()->ExecuteS('
|
||||
INSERT INTO `'._DB_PREFIX_.'category_product` VALUES (
|
||||
'.(int) $id_category_default.',
|
||||
'.(int) $id_product.',
|
||||
'.(int) $pos.'
|
||||
)
|
||||
');
|
||||
}
|
||||
}
|
||||
|
||||
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
|
||||
}
|
||||
|
||||
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
|
||||
} elseif(Tools::isSubmit('submitUploadAccessories')) {
|
||||
}
|
||||
elseif(Tools::isSubmit('submitUploadAccessories')) {
|
||||
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
|
||||
fgetcsv($f, 0, ';');
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user