Prestashop 1.5.6.2 shows wrong products in category (FrontOffice)
I am using prestashop 1.5.6 and doing bulk category and product manipulation in mysql through 3rd party libraries (which I wrote). All is well until some of my products show up in the wrong category. For example, I have product A in category A; But when I go to Product B of Category A, there is also. I think there is something wrong with my table ps_category_product
with position column. I am updating this table with the code below;
$sqlFirst = 'SELECT id_product, id_category_default, xml_id_product FROM ps_shop_product';
$queryFirst = $db->prepare($sqlFirst);
$queryFirst->execute();
while ($rowFirst = $queryFirst->fetch(PDO::FETCH_OBJ)){
$sqlProductAddCatPosFirst = '
INSERT INTO ps_shop_category_product
(id_product, id_category, position)
VALUES
(?, ?, ?)
';
// ps_shop_category_product Sql sorgumuzu hazırlayalım
$queryProductAddCatPosFirst = $db->prepare($sqlProductAddCatPosFirst);
$queryProductAddCatPosFirst->bindParam(1, $rowFirst->id_product, PDO::PARAM_INT);
$queryProductAddCatPosFirst->bindParam(2, $rowFirst->id_category_default, PDO::PARAM_INT);
$queryProductAddCatPosFirst->bindParam(3, $rowFirst->id_product, PDO::PARAM_INT);
// ps_shop_category_product Hazır Sql sorgumuzu çalıştıralım
$queryProductAddCatPosFirst->execute();
}
But everything is fine on the tab Backoffice > Products > Filter By Category
. It shows the correct products under the category. Are there any specific details in Front Office? If I truncate the table (ps_category_product)
, my products are not showing up in categories in Front Office. What am I missing?
Any help would be very helpful.
Update
Following @bcsteeve's comment, I create a sample category from BackOffice and all products listed in the correct categories. When I look at changes in my mysql tables; only the table ps_category
changed some of the column values nleft
and nright
.
In my simple web service, I assign nleft
and the nright
value 0 (zero). But now they have some values other than 0 (zero).
Now I think my problem is recalculating the table hierarchy ps_category
.
Is there any particular main controller and / or method that can recalculate the nleft and nright values in the table
ps_category
? Because I don't want to add a category manually after my webservice has updated my products and categories.
Thanks in advance!
source to share