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

Edit product list prestashop

Edit tpl code in product list and product detail ap page builder

As you might know, Ap Pagebuilder Module is using product list builder and product detail …

Leave a Reply

Your email address will not be published. Required fields are marked *