Draq
Draq

Reputation: 49

Prestashop add category with ID

I have small problem with adding categories to Prestashop via php script. One good guy helps me with script for adding category:

$cat = new Category();
$cat->id = 301;
$cat->id_category = 301;
$cat->id_category_default = 301;
$cat->name = 'category name';
$cat->active = 1;
$cat->link_rewrite = Tools::link_rewrite($cat->name);
$cat->id_parent = Configuration::get('PS_HOME_CATEGORY');
$cat->add();

As you can see a try id, id_category and category_default for setting category ID, but it doesn't work, because in databse it filling id auto_incrementaly.

How can I make category with own id? Thank you

Upvotes: 1

Views: 4088

Answers (2)

Edu Gasser
Edu Gasser

Reputation: 1

You can modify the API of webservice.

You must comment this lines of file WebserviceRequest.php

/*elseif ($this->method == 'POST' && count($ids) > 0)
{
    $this->setError(400, 'id is forbidden when adding a new resource', 91);
    return false;
}*/

Upvotes: 0

Altaf Hussain
Altaf Hussain

Reputation: 5202

Yes, it will not work, because you are using the PS CRUDs by calling add function. If you want to add your own auto incremented values, or IDs, then you should write your own queries for database insertion. It is easy in latest PS 1.5.x . If you are using PS 1.5.x, then do it like below.

$data = array(
          'id_category' => 301,
          'active' => 1,
  ...... and so one with other no language fields 

)

after that call the insert function

  Db::getInstance()->insert('category', $data); 

Now for language data insertion create an other array like below

 $dataLang = array (

'id_category' => 301,
'id_lang' => 1,
'name' => 'my category',

 ..... and so on

);

Now insert it also as above

 Db::getInstance()->insert('category_lang', $dataLang);

Please note that if you are using multiple languages, then you have to inert language data in a loop for all languages or insert them without loop for each language which ever is easy for you.

Note: The above code is not tested, and is just a sample code.

Thank you

Upvotes: 2

Related Questions