Add category prestashop programmatically

I would like to add categories programmatically in prestashop, I tried this code

$object = new Category(); $object->name = "xcvxvvx"; if (!$parent_id){ $parent_id = Configuration::get('PS_HOME_CATEGORY'); } $object->id_parent = $parent_id; $object->link_rewrite = array((int)(Configuration::get('PS_LANG_DEFAULT')) => $category); $object->add(); $object->id_category = $object->id; $object->id_category_default = $object->id; 

$ object-> update ();

I get this error message:

 Fatal error: Uncaught exception 'PrestaShopException' with message 'Property Category->name is empty' in /var/www/autospareparts.se.com/classes/ObjectModel.php:874 Stack trace: #0 /var/www/autospareparts.se.com/classes/ObjectModel.php(306): ObjectModelCore->validateFieldsLang() #1 /var/www/autospareparts.se.com/classes/ObjectModel.php(490): ObjectModelCore->getFieldsLang() #2 /var/www/autospareparts.se.com/classes/Category.php(157): ObjectModelCore->add(true, false) #3 /var/www/autospareparts.se.com/get_product.php(51): CategoryCore->add() #4 {main} thrown in /var/www/autospareparts.se.com/classes/ObjectModel.php on line 874 

error associated with the name field that I assigned

 $object->name = "xcvxvvx"; 

Thanks in advance

+4
source share
3 answers

This is due to internationalization. The ObjectModel class needs an array for the name, just like link_rewrite .

Working code (checked on 1.5.4.1, but should work on> = 1.5)

 $object = new Category(); $object->name = array((int)Configuration::get('PS_LANG_DEFAULT') => 'Cool name'); $object->id_parent = Configuration::get('PS_HOME_CATEGORY'); $object->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') => 'cool-url'); $object->add(); 
+7
source

I think this is the best solution that it handles set names and link_rewrite for multiple PS languages

 $object = new Category(); $link = Tools::link_rewrite( $category); $object->name = array(); $object->link_rewrite = array(); foreach (Language::getLanguages(false) as $lang){ $object->name[$lang['id_lang']] = $category ; $object->link_rewrite[$lang['id_lang']] = $link; } $object->id_parent = $parent_id; $object->save(); 
+1
source

When you try to add a category like this or such a product, you get such errors, I don’t know why, because I myself received such errors when importing data from another system, such as drupal. $ c is nothing more than another array of values ​​that has all the data associated with the category. You must evaluate the associated data with each element of the array.

You must do this as shown below:

  $data['id_parent'] = $c['id_parent']; $data['id_shop_default'] = 1; $data['active'] = $c['active']; $data['date_add'] = $c['date_add']; $data['date_upd'] = $c['date_upd']; $data['position'] = $c['position']; $datal['id_category'] = $id_category; $datal['id_shop'] = 1; $datal['id_lang'] = 1; $datal['name'] = pSQL($c['name']); $datal['description'] = pSQL($c['description']); $datal['link_rewrite'] = pSQL($c['link_rewrite']); $datal['meta_title'] = pSQL($c['meta_title']); $datal['meta_keywords'] = pSQL($c['meta_keywords']); $datal['meta_description'] = pSQL($c['meta_description']); $dataShop['id_category'] = $id_category; $dataShop['id_shop'] = 1; $dataShop['position'] = $c['position']; if(!DB::getInstance()->insert('category', $data)) die('Error in category insert : '.$c['id_category']); if(!DB::getInstance()->insert('category_lang', $datal)) die('Error in category lang insert : '.$c['id_category']); if(!DB::getInstance()->insert('category_shop', $dataShop)) die('Error in category shop insert : '.$c['id_category']); 
0
source

Source: https://habr.com/ru/post/1501482/


All Articles