开发者

Create product from a module in prestashop

开发者 https://www.devze.com 2023-03-13 21:00 出处:网络
I guys i try to create a custom product from a module with this code : $defaultLanguage = new Language((int)(Configuration::get(\'PS_LANG_DEFAULT\')));

I guys i try to create a custom product from a module with 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 开发者_Go百科the database but wasn't display in the back office, someone have an idea to solve this problem ?


The name and meta keyword field are both multi-language arrays. If you look at AdminImport.php in admin/tabs you'll find the definition for a function:

private static function createMultiLangField($field) 

Copy this function into your module and you can use it to create a suitable array for these multi-language fields if you call it by passing your text as the $field parameter (it will set the value for all languages to the string you pass in). You should also set a default value for the description_short and link_rewrite fields:

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

and

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

The second point is that although you've set the default category, you will also have to explicitly set id_category as an array e.g.

$object->category=array(18);

I also think you should then set the categories explicitly with:

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

It should then appear in the catalog.


You can refer to this example where the author created an import procedure to import products.

Custom Product Import

As you can see after download the ProductImporter.php is that the id_lang is added to each property.


to make the product available, You need to change this:

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

-rk-

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号