我的项目是使用Web服务添加类别的,当我刷新类别文件时,它会创建与以前相同的类别。
foreach ($XMLRSString->Families->Family as $family)
{
$categories = $family->Code;
$exists = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('select id_category from ps_category where id_category = "'.$categories['id_category'].'"');
$category = new Category((int)$categories);
$category->id= (int)$categories;
$category->id_category_default=(int)$family->Code;
$category->force_id = true;
$category->is_root_category = false;
$category->name = array((int)Configuration::get('PS_LANG_DEFAULT') => $family->Designation);
$category->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') => $family->Code);
$category->id_parent = Configuration::get('PS_HOME_CATEGORY');
if($exists){
$category->add();
echo "created ".$family->Code."<br>";
}
else{
$category->save();
echo "already exists ".$family->Code."<br>";
}
}
它没有出现任何错误,但没有显示在PrestaShop类别中
答案 0 :(得分:1)
无需使用if ($exists)
,因为$category->save();
将已经检查类别是否存在,如果存在则将对其进行更新,否则将创建该类别。
您可以参考有关此主题的上一个问题: How to test if a category already exists? PrestaShop
我认为您的问题源于您忘记将类别设置为活动状态的事实
$category->active = 1;
此外,您应该检查$category->save();
的返回值。
我希望这会有所帮助!
答案 1 :(得分:0)
I find the CURE
if($id_category=Db::getInstance()->getValue('SELECT id_category FROM '._DB_PREFIX_.'category WHERE id_category="'.pSQL($categories).'"')){
$category->save();
}
else{
$category->id= $categories;
$category->id_category_default=(int)$family->Code;
$category->force_id = true;
$category->add();
}