add category programmatically prestashop

时光怂恿深爱的人放手 提交于 2019-12-10 09:28:57

问题


I would like to add categories programmatically to 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 related to name field that i assigned

$object->name = "xcvxvvx";

Thanks in advance


回答1:


It's because of the internationalization. The ObjectModel class needs an array for the name, exactly like the link_rewrite.

Working code (tested 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();



回答2:


I think it's the best solution, that it handle set names and link_rewrite to multiple PS langages

$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();



回答3:


When ever you try to add category like this or a product like this you will get such errors, i dont know why as i myself got such errors while importing data from other system like drupal. $c is nothing just another array of values i have, which has all the category related data. You should assing your related data to each array element.

You should do it as 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']);


来源:https://stackoverflow.com/questions/18720880/add-category-programmatically-prestashop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!