如何测试一个类别是否已经存在?PrestaShop

我从其他数据库 Web 服务获取类别,并在刷新文件以添加类别时将它们放在 PrestaShop 上,我想确保类别 ID 是否存在,如果存在,我想更新类别。


$XMLRQString = '<?xml version="1.0" encoding="utf-8"?>'.

    '<x:Winmax4GetFamiliesRQ xmlns:x="urn:Winmax4GetFamiliesRQ">'.

    '</x:Winmax4GetFamiliesRQ >';


$return = $client->GetFamilies($Params);

$XMLRSString = new SimpleXMLElement($return->GetFamiliesResult);

if ($XMLRSString->Code > 0)

    echo '</br>Error: '.$XMLRSString->Code." ".$XMLRSString->Message;

else{


        foreach ($XMLRSString->Families->Family as $family)

        {   


            $category = new Category();


            $category->id = $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');


            $category->add();


        }

    }


小怪兽爱吃肉
浏览 188回答 2
2回答

阿晨1998

这会起作用:$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');$home = (int)Configuration::get('PS_HOME_CATEGORY');$category = new Category((int)$family->Code);$category->is_root_category = false;$category->name = array($default_lang => $family->Designation);$category->link_rewrite = array($default_lang => $family->Code);$category->id_parent = $home;$category->save();此外,您始终可以使用Validate::isLoadedObject()静态方法检查对象是否有效。
打开App,查看更多内容
随时随地看视频慕课网APP