我想以编程方式将类别添加到prestashop,我尝试了这段代码
$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;
$对象 - >更新();
我收到此错误消息:
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
与我指定的名称字段相关的错误
$object->name = "xcvxvvx";
提前致谢
答案 0 :(得分:8)
这是因为国际化。 ObjectModel
类需要一个名称数组,与link_rewrite
完全相同。
工作代码(在1.5.4.1上测试,但应该在> = 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();
答案 1 :(得分:2)
我认为这是最好的解决方案,它处理集合名称和link_rewrite到多个PS语言
$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();
答案 2 :(得分:0)
当你尝试添加这样的类别或这样的产品时你会得到这样的错误,我不知道为什么我自己从其他系统如drupal导入数据时会出现这样的错误。 $ c不仅仅是我拥有的另一个值数组,它包含所有与类别相关的数据。您应该将相关数据分配给每个数组元素。
你应该按照以下方式进行:
$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']);