Home / Tutorials / PrestaShop 1.6 Tutorials / Simple code to fix position of product in category after import

Simple code to fix position of product in category after import

If you have this bug in your prestashop:

all product same position = 1

and you can try to drag to sort. after save and reload position is not change.

follow my guide to fix

create a file in your root web name as correct_product_position.php:

require(dirname(__FILE__).'/config/config.inc.php');
$sql = 'select * from `'._DB_PREFIX_.'category_product`';
$data = Db::getInstance()->executeS($sql);
$category = array();
foreach ($data as $value) {
$category[$value['id_category']][] = $value;
}
foreach ($category as $catrow) {
$i = 0;
foreach ($catrow as $row) {
$sql = '
UPDATE `'._DB_PREFIX_.'category_product` cp
SET cp.`position` = '.(int)$i.'
WHERE cp.`id_product` = '.(int)$row['id_product'].'
AND cp.`id_category`='.(int)$row['id_category'];
Db::getInstance()->execute($sql);
echo $sql;
$i++;
}
}

after that
you run https://yoursite.com/correct_product_position.php
this code will help you correct position
see my result: like a dream

About Leo Theme

Check Also

Leotheme Black Friday 2022

Prestashop Theme and Module Black Friday and Cyber Monday Sales Leotheme 2022 30% Off

Prestashop Leotheme Black Friday 2022 Promotion! Dear valued customers, Black Friday and Cyber Monday Sales …

Leave a Reply