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

时间:2016-01-15 13:12:01

标签: php prestashop prestashop-1.6

我正在尝试编写一个脚本,将我的类别从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,我希望有人可以向我解释我做错了什么..

2 个答案:

答案 0 :(得分:0)

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

阵列很少从1开始......改变它:

$类别 - &GT;名称[0] = ... $分类 - &GT; link_rewrite [0] = ...

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

答案 1 :(得分:0)

它对我有用

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