PrestaShop 1.7.4.2
First of all, I'm sorry for my english, I hope that it's understandable
During import product with force ID option, it's impossible to change the category_default with the new category
I tried to import few products with a CSV file, and I wanted to force ID to keep products how the supposed to be with a full import. I found out that the new category wasn't set in _id_category_default_ but just in the _id_category_. It means that in my products list, the old category was still set, and worst, in my breadcrumb too....
I looked in the code and I found out that the import controller didn't allow to change the _id_category_default_ if it was already set !
Solution 1 : Adding a column in import file with default value
Adding a option with default value might be the best solution.
It won't change the behavior for those who wants to keep the current way to do, but it will allow those who want to change the _id_category_default_ to do it :)
For example, a new column (0 = false (default), 1 = true) to choose if you want that the first new category of the import file replace the current category default of the product. If the column isn't found, the _id_category_default_ won't be replaced.
Solution 2 : Create a checkbox in the form
Instead of changing the import file format, it can be possible to add a checkbox in the import form to let user choose to change _id_category_default_ with the new category.
For both solution, it has to force the first category if you set multiple in your import file !!
Current fix : How I did it
I created a module which modify the function in file : AdminImportController / function productImportOne()
This is the actual code which not allow to change _id_category_default_ if it's already set once :
if (!isset(product->id_category_default) || !$product->id_category_default) {
// this if will avoid ereasing default category if category column is not present in the CSV file (or ignored)
if (isset($product->id_category[0])) {
$product->id_category_default = (int)$product->id_category[0];
} else {
$defaultProductShop = new Shop($product->id_shop_default);
$product->id_category_default = Category::getRootCategory(null, Validate::isLoadedObject($defaultProductShop)?$defaultProductShop:null)->id;
}
}
This code allows to change to always set the first category of the imported product as _id_category_default_
if (isset($product->id_category[0])) {
$product->id_category_default = (int)$product->id_category[0];
} else {
if (!isset($product->id_category_default) || !$product->id_category_default) {
$defaultProductShop = new Shop($product->id_shop_default);
$product->id_category_default = Category::getRootCategory(null, Validate::isLoadedObject($defaultProductShop) ? $defaultProductShop : null)->id;
}
}
My code doesn't allow to keep the current behavior, but it can be changed easily.
I hope it helps !
Have a nice day :)
Hi @itisco,
Thanks for your report.
I manage to reproduce the issue with PS 1.7.4.3.
Would you be willing to make a pull request on GitHub with your code suggestion?
https://github.com/PrestaShop/PrestaShop/tree/develop
Thank you!
Awesome, works for me.
Thanks, so much time trying to get it.