从Prestashop中的模块创建产品 [英] Create product from a module in prestashop
问题描述
我尝试使用以下代码从模块中创建自定义产品:
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 the database but wasn't display in the back office, someone have an idea to solve this problem ?
推荐答案
name和meta关键字字段都是多语言数组.如果您在admin/tabs中查看AdminImport.php,则会找到函数的定义:
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)
将此函数复制到模块中,如果通过将文本作为$field
参数传递来调用它,则可以使用它为这些多语言字段创建合适的数组(它将所有语言的值设置为您传入的字符串).您还应该为description_short
和link_rewrite
字段设置默认值:
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')) => '');
和
$object->link_rewrite = array((int)(Configuration::get('PS_LANG_DEFAULT')) => '');
第二点是,尽管您已设置了默认类别,但还必须将id_category显式设置为数组,例如
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.
这篇关于从Prestashop中的模块创建产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!