1

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

2
  • Why are you trying it insert an auto increment value? Mar 7, 2013 at 8:46
  • Because I am working on XML import script which using parnet id category, so it's easier for me to have same id. And if I delte auto_increament in database, it doesn't work.
    – Draq
    Mar 7, 2013 at 9:46

2 Answers 2

2

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

2
  • Hello, thank you very much! I have one more question. How can i found if elemnt of array is requied? I do same think for products and there is big number of array elements and i like to know what is required and what no. Than you very muhc!
    – Draq
    Mar 10, 2013 at 15:54
  • Hello, You can find it in two ways, either in the table structure, or in the Category class. If you open the category class in classes/category.php you will have an array named public static $definition. In this array, all the category fields will be defined in array format. Checkout the fields, and you will see that some of them has an element of 'required' => true , that is the required field. Hope it will help. Mar 11, 2013 at 6:29
0

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;
}*/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.