Jump to content

[ask] using category class to create new category, database updated but admin page show nothing


Recommended Posts

hello,

i need to create a code to add new category / products,

i've been trying to use category class, here is the code:

$cat = new Category(NULL, NULL);
$cat->name = "test";
$cat->active = true;
$cat->description = "Testing purposes category";
$cat->id_parent = 0;

if ($cat->validateFields() === true) {
   if ($cat->add()) {
       echo "success";
   }
   else {
       echo "unsuccess";
   }
}



the new category has been added into database, but i cant see it in the admin page..

does anyone know something about it?
is there anything wrong in my code?

Link to comment
Share on other sites

Hi !

id_parent must be equal to 1
active must be equal to 1
name and description are multi-language fields, so you must specify in which lang you set the name.

This code should work :

$defaultLanguageId = intval(Configuration::get('PS_LANG_DEFAULT'));

$cat = new Category(NULL, NULL);
$cat->name[$defaultLanguageId] = "test";
$cat->active = 1;
$cat->description[$defaultLanguageId] = "Testing purposes category";
$cat->id_parent = 1;

if ($cat->validateFields() === true) {
   if ($cat->add()) {
       echo "success";
   }
   else {
       echo "unsuccess";
   }
}

Link to comment
Share on other sites

  • 7 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...