Jump to content

Create a new product, category not updated


CptSilver

Recommended Posts

Hello,

 

I have see some posts about creating a product or about the category so a copied the code and now have this : 

 

$defaultLanguage = new Language((int)(Configuration::get('PS_LANG_DEFAULT')));  
/* Add a new product */
$object = new Product();
$object->price = 32;
$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));
 
$object->save();
 
So, my product is created with all info BUT the category.
 
When i test the data of addToCategories he do that test
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 for me $new_id_categ is in $current_categories, so the thing never add it.
 
Anyone have an idea?
 
Thanks
 
 
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...