Create a product from a module in preashashop

I guys are trying to create a custom product from a module using this code:

$defaultLanguage = new Language((int)(Configuration::get('PS_LANG_DEFAULT')));  
/* Add a new product */
$object = new Product();
$object->price = 22;
$object->id_tax_rules_group = 0;
$object->name = 'test';
$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->width = 0;
$object->height = 0;
$object->depth = 0;
$object->weight = 0;
$object->out_of_stock = 0;
$object->active = 0;
$object->id_category_default = 18;
$object->category = 18;
$object->available_for_order = 0;
$object->show_price = 1;
$object->on_sale = 0;
$object->online_only = 1;
$object->meta_keywords = 'test';
if($object->save())
    $object->add();
echo "produit ajouté";

The code works fine, the product was added to the database, but did not appear in the back office, does anyone have an idea to solve this problem?

+5
source share
3 answers

Keyword name and meta fields are multilingual arrays. If you look at AdminImport.php in admin / tabs, you will find a definition for the function:

private static function createMultiLangField($field) 

, , , $field ( , ). description_short link_rewrite:

$object->description_short = array((int)(Configuration::get('PS_LANG_DEFAULT')) => '');

$object->link_rewrite = array((int)(Configuration::get('PS_LANG_DEFAULT')) => '');

, , , id_category , .

$object->category=array(18);

, :

$object->updateCategories($object->category, true);

.

+12

, .

ProductImporter.php, , id_lang .

0

To make the product available, you need to change this:

$object->active = 1; // sets the product as active for shop

-rk -

-1
source

All Articles