polykodesas Posted July 14, 2015 Share Posted July 14, 2015 Hi, i'm Trying to create a new category and a new product and to assign the new category to the new product. When check the BO product's detail -> association, i can see that my product is well associate to the category, and the default category has been well set as well, BUT on the BO product's list the category is not displayed (i got: --) and when i try to search the product on the FO, it return "no result". To solve that i should go back to BO product's detail -> association and click on save button. Then everything is ok. So i'm pretty sure i miss something in my code :-/ could anyone help ? here is my code: // category creation $categories = new Category; $categories->name = array($this->context->language->id => $groupeCategorie); $categories->link_rewrite = array($this->context->language->id => $this->sanitize($groupeCategorie)); // sanitize is a private function who remove accents, spaces, etc... $categories->id_parent = Category::getRootCategory()->id; $categories->add(); $categories->id_category_default = $categories->id; $categories->save(); $product_id_category_default = $categories->id; // product creation $tempProd = new Product; $tempProd->name = array((int)Configuration::get('PS_LANG_DEFAULT') => trim($product['name']); $tempProd->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') => trim($product['name']); $tempProd->ean13 = trim($product['gencode']); $tempProd->quantity = trim($product['dispo']); $tempProd->ecotax = trim($product['eco_part_ttc']); $tempProd->weight = trim($product['poids']); $tempProd->baseprice = trim($product['tht1']); $tempProd->price = trim($product['tttc1']); $tempProd->active = true; $tempProd->id_default_category = array($product_id_category_default); $tempProd->category=array($product_id_category_default); $tempProd->add(); $tempProd->updateCategories($tempProd->category,true); i tryed to replace "updateCategories" by "addToCategories" by same issue :-/ Thanks in advance for help. Sébastien Link to comment Share on other sites More sharing options...
musicmaster Posted July 14, 2015 Share Posted July 14, 2015 I am not an expert in the PS functions. So I can only make an educated guess. A category is only assigned a category id once it is saved in the database as the category id is an auto increase integer that is assigned by the database. So I doubt whether the category id you are using is a valid one. Did you check for that? Link to comment Share on other sites More sharing options...
polykodesas Posted July 14, 2015 Author Share Posted July 14, 2015 Hi musicmaster, Actually i pasted here an extract of my code, the real code have a test to not recreate category: $level1catSearch = Category::searchByNameAndParentCategoryId($this->context->language->id, $groupeCategorie, 2); if (!$level1catSearch) { // category creation $categories = new Category; $categories->name = array($this->context->language->id => $groupeCategorie); $categories->link_rewrite = array($this->context->language->id => $this->sanitize($groupeCategorie)); // sanitize is a private function who remove accents, spaces, etc... $categories->id_parent = Category::getRootCategory()->id; $categories->add(); $categories->id_category_default = $categories->id; $categories->save(); $product_id_category_default = $categories->id; } else { $product_id_category_default = $level1catSearch['id_category']; } Link to comment Share on other sites More sharing options...
musicmaster Posted July 14, 2015 Share Posted July 14, 2015 Hi polykodesas, As I wrote I am not an expert on this issue but my impression is that you may have assigned a not existing category id. So it won't help if you publish more of your code. What I suggest instead is that you insert some debugging code ("echo category_id") to see whether my theory is right. Link to comment Share on other sites More sharing options...
polykodesas Posted July 14, 2015 Author Share Posted July 14, 2015 yes a did it already, i tried to force my variable to ans existing (fir sure) category, bu no success Link to comment Share on other sites More sharing options...
polykodesas Posted July 14, 2015 Author Share Posted July 14, 2015 (edited) I answered to myself with a solution (if it can help) here the code to use to create a product and assign catégory: $tempProd = new Product; $tempProd->name = $your_product_name $tempProd->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') => $your_product_name_without_accents_or_spaces_or_special_char); $tempProd->price = $your_price $tempProd->description = $your_product_description $tempProd->active = true; $tempProd->id_category_default = (int)$product_id_category_default; // force default category id to be an integer // First add the product $tempProd->add(); // Then assign it to catégories $tempProd->updateCategories($catListArr,true); $catListArr is an array containing all the catégorie to assign the product on. exemple: your catégorie tree is like home (id 2) | --- Books (id 10) | | | ---- Comics (id 14) | ---- Horror (id 15) | --- Movies ( id 23) if you want your product to be in home -> Books -> Horror do $tempProd->updateCategories(array(2,10,15),true); and of course the default category id will be the deeper one in the tree (15 in our exemple) then after adding the product the seash engine index need to be regenerated. you can do it by t3 ways: 1 - use menu Preference -> search -> indexation and click "rebuild index" link 2 - create a cron task with the link given at the same place 3 - add the following code after product creation ini_set('max_execution_time', 7200); Search::indexation(Tools::getValue('full')); Sebastien. Edited July 14, 2015 by polykodesas (see edit history) 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now