我正在尝试编写一个脚本,将我的类别从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,我希望有人可以向我解释我做错了什么。
它
对我有用
$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 这些是控件插入
必须删除在link_rewrite中无效的字符。删除标点符号、带重音符号的元音、括号...在prestashop代码中,有一个函数可以将字符串转换为link_rewrite。
数组很少从 1 开始...更改它:
$category->名称[0]=...$category->link_rewrite[0]=...
PD:在prestashop中操作数据的最佳方式是Rest API