Reputation: 105
In my module clients can add products to the shop, it will pass by a separate database. When an admin check the products and validate one, i change the information to a prestashop product.
So i have this code :
$object = new Product();
$object->price = 32;
$object->id_tax_rules_group = 0;
$object->name = array((int)Configuration::get('PS_LANG_DEFAULT') => $creation['title']);
$object->id_manufacturer = 0;
$object->id_supplier = 0;
$object->quantity = 1;
$object->minimal_quantity = 1;
$object->additional_shipping_cost = 0;
$object->wholesale_price = 0;
$object->ecotax = 0;
$object->out_of_stock = 0;
$object->available_for_order = 1;
$object->show_price = 1;
$object->on_sale = 1;
$object->online_only = 1;
$object->meta_keywords = $creation['title'];
$object->active = 1;
$object->description_short = array((int)(Configuration::get('PS_LANG_DEFAULT')) => $creation['description']);
$object->link_rewrite = array((int)(Configuration::get('PS_LANG_DEFAULT')) => $creation['title']);
$object->id_category = 2;
$object->id_category_default = 2;
$object->addToCategories(array(2,13));
//SAVE
$object->save()
The product is saved and has the good information BUT it's not associated to any category. When i go to my page products, where i can see all products, i see my product and the category is there BUT when i enter the details of the product and i go to the category list nothing is check.
i tried to see more in the code what is happenning and i saw this :
if (!in_array($new_id_categ, $current_categories))
$product_cats[] = array(
'id_category' => (int)$new_id_categ,
'id_product' => (int)$this->id,
'position' => (int)$new_categ_pos[$new_id_categ],
);
And after test the "if" is always true and so the product is association is never created. I have check and this association does not exist.
I don't know what to do next, or if maybe i need to not use addToCategory, and enter maybe the data myself...
So please help thanks.
Upvotes: 1
Views: 3469
Reputation: 3118
The method addToCategories() assigns categories to already created product. In other words you need to call the save() method of the Product class before calling addToCategories();
Upvotes: 1