orwell Posted July 8, 2012 Share Posted July 8, 2012 (edited) Hi ! I'm writing a custom module and I need to create new categories from there. I'm wondering which is the best and easiest way to create a new category from a script. I can't find a method in the Category class to do so... Maybe someone has a snippet ready to use. Thank you Edited July 9, 2012 by orwell (see edit history) Link to comment Share on other sites More sharing options...
bellini13 Posted July 9, 2012 Share Posted July 9, 2012 Category extends ObjectModel which has an add function. Basically just instantiate a new category object and call add(), then set your attributes and perform an update. Should do it Link to comment Share on other sites More sharing options...
orwell Posted July 9, 2012 Author Share Posted July 9, 2012 Thank you for pointing this out. I had some trouble to set the translated fields but I found a way to do it. $category->description = array((int)(Configuration::get('PS_LANG_DEFAULT')) => "here is my description"); Link to comment Share on other sites More sharing options...
orwell Posted July 9, 2012 Author Share Posted July 9, 2012 By the way is there a simple way to get the ID of the object created ? For now I make a DB query to retrieve it, but maybe there's a better way. Link to comment Share on other sites More sharing options...
bellini13 Posted July 9, 2012 Share Posted July 9, 2012 yes, each class that extends ObjectModel has an attribute named id. So after you perform the add, you can access the id by doing this $id = $category->id; Link to comment Share on other sites More sharing options...
kerwin Posted July 10, 2012 Share Posted July 10, 2012 yes,i agree with you . Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now