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 admin@leotheme.com

Check Also

Create-Mobile-Theme-and-Tablet-Theme

Create Mobile theme and Tablet theme on Prestashop – Ap Pagebuilder Module

In the previous blog, we introduced you the newly updated features of the Ap Pagebuilder …

Leave a Reply

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