在为actionProductAdd创建一个钩子然后从后端添加产品后,版本1.7发生错误,如下所示:
哎呀!发生错误服务器返回“500内部服务器错误”。有些东西坏了。发生此错误时,请告诉我们您在做什么。我们会尽快修复它。给您造成的任何不便,请原谅。
下面是我的模块文件夹文件钩子的代码
<?php
class XyzData extends Module {
public static $executed = false;
public function __construct() {
parent::__construct();
}
public function install() {
return parent::install() && $this->registerHook('actionProductSave');
}
public function uninstall() {
}
public function hookActionProductSave($params) {
echo "Calling function"; exit;
}
}
您需要先创建模块,然后在该模块中调用actionProductSave
。你会找到钩子的详细清单here。
用于创建模块;例如,我的模块(my_module)您需要按照以下步骤操作。
1)在my_module
目录中创建文件夹modules
。
2)在my_module.php
目录中添加文件logo.png
和my_module
文件。安装模块时将自动创建config.xml
文件。
3)在'my_module.php'文件中添加以下代码。你可以在名为hookActionProductSave
的函数中做你的东西。每次产品保存时都会调用此函数。
if (!defined('_PS_VERSION_')) {
exit;
}
class My_Module extends Module
{
public function __construct()
{
$this->name = 'my_module';
$this->author = 'Divyesh Prajapati';
$this->version = '1.0.0';
$this->need_instance = 1;
$this->tab = 'administration';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans('My Module', array(), 'Modules.MyModule.Admin');
$this->ps_versions_compliancy = array('min' => '1.7.1.0', 'max' => _PS_VERSION_);
}
public function install()
{
return parent::install() && $this->registerHook(['actionProductSave']);
}
public function uninstall()
{
return parent::uninstall();
}
public function hookActionProductSave($params) {
$product_id = $params['id_product']; // Product Id
$product = $params['product']; // Product Object
// Do your stuffs here
}
}
4)现在进入管理员>模块>模块和服务;在那里去选项卡选择并找到模块my_module
并安装它。
5)现在,只要您的产品从管理面板中保存,你在hookActionProductSave
写的东西将被调用。当产品被保存时,请找到你在$params
数组中获得的数组的附图.
希望这可以帮助!
钩子actionProductSave
不是为了呈现内容,而是为了处理你身边的产品数据。