Jump to content

Get the id of a category created with "new Category"


Recommended Posts

Hello,

Just after creating a category, i'd like to retrieve its id : 

--

function create_new_category($cat_name, $id_parent) {
    $category = new Category;
    $category->active = 1;
    $category->id_parent = $id_parent;
    $category->name = [ 1=> $cat_name];
    $category->is_root_category = false;
    $category->link_rewrite = [ 1=> Tools::str2url($cat_name)];
    $category->meta_description = [ 1=> $cat_name ];
    $re = $category->add();

    if($re)  {
         return $category->id_category;
    } else {
        unset($re);
        unset($category);
        log("error creating ". $cat_name);

   }
}

--

But it doesn't work, "$category->id_category" is empty

For the moment, I make a sql request to retrieve the highest id of the ps_category table, which should normally correspond to the id of the last category created, but I don't feel it's very clean.
Can I do better?
 

Best regards

Link to comment
Share on other sites

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...