Jump to content

Adding a Category - prestashop says name is missing


Recommended Posts

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?

 

 

 

 

Link to comment
Share on other sites

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

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:

image.thumb.png.ef075a3e38cba9e3b4126b3985eef779.png

But I'd prefer a more elegant solution with Prestashop's own methods, if possible.

Link to comment
Share on other sites

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 by QuickUpdate.net (see edit history)
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...