Reputation: 142
I am trying to write a script that imports my categories from a XML into prestashop. There is one problem, the script does not add the category and stops working when it reaches ->add();
I'm trying to find the problem, but I really don't know what to do anymore.
Here is my script:
if ($cat == "") {
$category = new Category;
$category->active = 1;
$category->id_parent = 3;
$category->name[1] = $product->category_name;;
$category->link_rewrite[1] = Tools::link_rewrite($product_xml->category_name);
echo "<br />name of new category = $product->category_name <br /> <br />";
$category->add();
$G_array[] = $category->id;
$G_cat = $G_cat . "\r\n" . 'Category: ' . $category->name[1].' with the id: '.$category->id.' Created'; //adding new category to var, to be displayed at the index page.
}else{
I am using prestashop 1.6, I hope that someone can explain to me what I am doing wrong..
Upvotes: 0
Views: 1310
Reputation: 93
It works for me
$Category = new Category();
$Category->name = [(int)Configuration::get('PS_LANG_DEFAULT') => 'nome'];
$Category->link_rewrite = [(int)Configuration::get('PS_LANG_DEFAULT') => 'link_rewrite'];
$Category->description = [(int)Configuration::get('PS_LANG_DEFAULT') => 'descrizione'];
$Category->active = 1;
$Category->id_parent = 3;
$Category->add();
for update
$Category = new Category($id_category);
$Category->name = [(int)Configuration::get('PS_LANG_DEFAULT') => 'nome'];
$Category->link_rewrite = [(int)Configuration::get('PS_LANG_DEFAULT') => 'link_rewrite'];
$Category->description = [(int)Configuration::get('PS_LANG_DEFAULT') => 'descrizione'];
$Category->active = 1;
$Category->id_parent = 3;
$Category->update();
you can check if the category is valid with this function
Validate::isLoadedObject($Category) //true or false
remember prestashop checks whether the values of the changes are valid
'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 128),
'link_rewrite' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isLinkRewrite', 'required' => true, 'size' => 128),
'description' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
isCatalogName, isLinkRewrite, isCleanHtml these are controls insertion
Upvotes: 1
Reputation: 54
You must delete character not valid in link_rewrite. Delete punctuation marks, vowels with accents, brackets, ... In prestashop code there is a function to convert a string to link_rewrite.
Rarely does the array starts at 1... Change it:
$category->name[0]=... $category->link_rewrite[0]=...
PD: The best way to manipulate data in prestashop is the Rest API
Upvotes: 0