Prestashop:在为 actionProductAdd 创建一个钩子并从后端添加产品后,它给出了一个错误

Prestashop: after making a hook for actionProductAdd and adding the product from the backend it gives an error

在为 actionProductAdd 创建钩子然后从后端添加产品后,版本 1.7 发生错误,如下所示:

Oops! An Error Occurred The server returned a "500 Internal Server Error". Something is broken. Please let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.

下面是我的模块文件夹文件钩子代码

<?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 不是呈现内容,而是从您这边处理产品数据。

您需要先创建模块,然后在该模块中调用 actionProductSave。您将找到可用挂钩的详细列表 here

用于创建模块;例如 My Module (my_module) 您需要按照以下步骤操作。

1) 在 modules 目录中创建文件夹 my_module

2) 在my_module目录下添加文件my_module.phplogo.png文件。 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) 现在转到 管理 > 模块 > 模块和服务;在其中转到选项卡 Selection 并找到模块 my_module 并安装它。

5) 现在只要您的产品将从管理面板保存;您在 hookActionProductSave 中编写的内容将被调用。当产品将被保存时,请找到您在 $params 数组中获得的数组的附件图像。

希望对您有所帮助!