I get the category from other database web service and I put them on PrestaShop when I refresh the file to add categories I wanna make sure if the category id exists, if exist I wanna update the category.
$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();
}
}
This would work:
Also, you can always check if an object is valid using the
Validate::isLoadedObject()
static method.In general, If you have large data (and also safe data), it's better to insert your data directly to your database. Use Db class instead of Category class.
Otherwise, you must use a save() method instead of add() method