Ikoshop Posted December 11 Share Posted December 11 (edited) I have a function attached to a hook which should add a category $category = new Category(); $category->name = 'foobar'; $category->active = true; $category->id_parent = 2; $category->level_depth = 2; $category->add(); When I click the button, the function runs but returns this error Property Category->name is empty. I double-check by logging the value of $category->name before the add() call and it's correct. Why would it give this error? Edited Thursday at 10:51 AM by Ikoshop solved (see edit history) Link to comment Share on other sites More sharing options...
Ikoshop Posted December 11 Author Share Posted December 11 Going off the definition of the Category class, if I replace the add(); by Hook::exec('actionCategoryAdd', ['category' => $category]); then it adds the category and category shop entries to the database but not the category_lang entry, so that's a bit useless. Might as well just insert the three tables manually I guess. Bit weird to have these functions that don't do what they're supposed to though Link to comment Share on other sites More sharing options...
Nickz Posted December 11 Share Posted December 11 What are you trying to accomplish? What addon, module or theme you wish to add? Link to comment Share on other sites More sharing options...
Ikoshop Posted December 11 Author Share Posted December 11 Thank you. It's my own module. I'm trying to sychronise products between Prestashop and a POS database. In this particular instance a new category has been added to the POS database and when I click a button in Prestashop it should get this new info from the POS DB and add a new category to the Prestashop DB. The SQL request equivalent of what I want to do is (roughly) this: But I'd prefer a more elegant solution with Prestashop's own methods, if possible. Link to comment Share on other sites More sharing options...
Prestashop Addict Posted December 11 Share Posted December 11 Hi, the name is required filed of the class Link to comment Share on other sites More sharing options...
Ikoshop Posted December 11 Author Share Posted December 11 Yes, that's why I added the name and checked that the name was set before calling add() . Have I done it wrong? Link to comment Share on other sites More sharing options...
QuickUpdate.net Posted December 11 Share Posted December 11 (edited) regarding your first question, in some versions of prestashop (>1.6) the name field is actually an array with the indexes being the language id for the respective string so in your case what may work is: $category->name = [1 => 'foobar', 2=> 'foobar_2'], "1" and "2" being the language ids you want to update, can be only one or more / but must be at least you default shop language id Edited December 11 by QuickUpdate.net (see edit history) 1 Link to comment Share on other sites More sharing options...
Prestashop Addict Posted December 11 Share Posted December 11 Yes change your code to $category->name[$idLang]; 1 Link to comment Share on other sites More sharing options...
Ikoshop Posted Thursday at 10:50 AM Author Share Posted Thursday at 10:50 AM Excellent, thank you! For anyone else wondering, the same is true for the obligatory field link_rewrite 1 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