5

我想以编程方式将类别添加到 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";

提前致谢

4

3 回答 3

13

这是因为国际化。该类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();
于 2013-09-10T14:47:31.133 回答
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();
于 2014-02-02T15:41:14.787 回答
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']);
于 2013-09-10T15:40:32.530 回答