为什么在 Prestashop 中以编程方式添加类别时出现错误?

Why do I get an error when adding a category programmatically in Prestashop?

我正在尝试编写一个脚本,将我的类别从 XML 导入到 prestashop。有一个问题,脚本没有添加类别并且在到达 ->add();

时停止工作

我正在努力寻找问题所在,但我真的不知道该怎么办了。

这是我的脚本:

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{

我正在使用 prestashop 1.6,我希望有人能向我解释我做错了什么..

您必须删除 link_rewrite 中无效的字符。删除标点符号、带重音符号的元音、括号、... 在 prestashop 代码中有一个将字符串转换为 link_rewrite.

的函数

数组很少从 1 开始...更改它:

$类别->名称[0]=... $类别->link_rewrite[0]=...

PD:在 prestashop 中操作数据的最佳方式是 Rest API

对我有用

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

待更新

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

您可以使用此功能检查类别是否有效

Validate::isLoadedObject($Category) //true or false

记住 prestashop 检查更改的值是否有效

'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这些是控件插入