php - 从 prestashop 中的模块创建产品

标签 php prestashop

伙计们,我尝试使用以下代码从模块创建自定义产品:

$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é";

代码工作正常,产品已添加到数据库但未在后台显示,有人有解决此问题的想法吗?

最佳答案

名称和元关键字字段都是多语言数组。如果您查看 admin/tabs 中的 AdminImport.php,您会找到一个函数的定义:

private static function createMultiLangField($field) 

将此函数复制到您的模块中,如果您通过将文本作为 $field 参数来调用它(它将设置所有语言的值到您传入的字符串)。您还应该为 description_shortlink_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);

然后它应该出现在目录中。

关于php - 从 prestashop 中的模块创建产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6385695/

相关文章:

php - php.ini 中的默认 php mysqli 连接

php - 为我的网站制作一个高效的搜索引擎

html - “主页”类别页面在更新后无法正常工作

php - Codeigniter 文件上传错误

php - 内部连接的问题

PHP 表单接受空白字段登录

docker - 如何运行具有持久数据的 Prestashop docker 容器?

security - Prestashop 1.7 中的 request.CRITICAL 和 security.INFO 错误

php - 在 prestashop 的 Hook.php 中具有函数 exec() 的目的是什么

mysql - Prestashop 1.4 - 如何对每个请求执行初始 MySQL 查询