Jump to content

PrestaShop 8.1.7 - Hooks failing to register


Oln25689

Recommended Posts

Hi all,

I am working on a plugin, that we are upgrading to make it work on 8.x of PrestaShop, up from 1.7.8.

The app itself seems to mostly be working, however one thing I have noticed is that is fails to register any of the Hooks for our plugin. This breaks the core functionality of our plugin.

Our code is the following;

    public function install()
    {
        Logger::addLog('Installing tabs', 2);
        $this->installTab();
        Logger::addLog('calling parent::install() and registering hooks', 2);
        $this->registerHooks();
        Logger::addLog('Registered Hooks and returning parent::install', 2);
        return parent::install();
    }
    private function registerHooks()
    {
        Logger::addLog('in registerHooks', 2);
        $common_hooks = array(
            'displayOrderConfirmation',
            'displayHeader',
            'postUpdateOrderStatus',
            'displayBackOfficeHeader'
        );
        $hooks_v17 = array('displayBeforeBodyClosingTag', 'filterProductSearch'); // v1.7 +
        $hooks_v16 = array('displayFooter');                                      // v1.6 and older
        $hooks = array_merge($common_hooks, version_compare(_PS_VERSION_, '1.7', '<') ? $hooks_v16 : $hooks_v17);
        foreach ($hooks as $hook) {
            Logger::addLog('Register the hook ' . $hook, 2);
            if (!Hook::getIdByName($hook)) {
                Logger::addLog('module: ' . $hook . ' hook was not found', 2);
                return false;
            }
            $res = $this->registerHook($hook);
            Logger::addLog('Result of registering hook ' . $res, 2);
            if (!$res) {
                Logger::addLog('module: Failed to register ' . $hook . ' hook', 2);
                return false;
            }
        }

        // This hook is not listed in hooks table, therefore we just register it without a check
        if ($this->registerHook('actionOrderHistoryAddAfter') === null) {
            Logger::addLog('module: Failed to register actionOrderHistoryAddAfter hook', 2);
        }

        return true;
    }

The logs I am seeing are: 

  • calling parent::install() and registering hooks
  • in registerHooks
  • Register the hook displayHeader 
  • Result of registering hook
  • module: Failed to register displayHeader hook
  • Registered Hooks and returning parent::install

Upon installing, there are no hooks that are registered correctly. There is no `displayHeader` hook being registered.

image.thumb.png.67348d53e797e24e4102c981678f27a7.png

Side note; I am also seeing intermittently an error when uploading the package into our PrestaShop store, it fails, then succeeds, then fails with the message: 

"Installation of module X failed. Unfortunately, the module did not return additional details."

Looking in the Apache error_log, the only error I can see is this:

"Got error 'PHP message: PHP Notice: Unknown: file created in the system's temporary directory in Unknown on line 0', referer: https://<prestashop-store>.com/administration/index.php/improve/modules/manage?_token=<token"

If anyone has any ideas, would be grateful.

I'm not sure on the easiest way to debug this - I'm new to PHP, but can't see any return type/reason for the hooks failing to register.

One thing that is interesting that I have found is this thread: 

It says that in V8, PrestaShop handles Hook errors where there are no methods implemented for it. But I have one implemented?

I've tried;

  • Going through all the upgrade deprecations between 1.7.8 to 8.X and checking we don't hit any of them
  • Just trying to register one hook, and not returning false.
  • Split out return parent::install() && $this->registerHooks(); into 2 lines.
  • Testing done on a Bitnami PrestaShop 8.1.7 store.

Thanks in advance

 

Edited by Oln25689
Typos (see edit history)
Link to comment
Share on other sites

Hi Knowband Plugins,

Thanks for responding, much appreciated. 

The `__construct` function as below;

public function __construct()
    {
        $this->name                   = 'module_name';
        $this->tab                    = 'checkout';
        $this->author                 = 'module_author';
        $this->need_instance          = 0;
        $this->ps_versions_compliancy = array(
            'min' => '1.5',
            'max' => _PS_VERSION_ > '1.7' ? _PS_VERSION_ : '1.7'
        );
        $this->bootstrap  = true;
        $this->module_key = '#{ModuleKey}';

        $this->httpClient = new ModuleHttpClient(ModuleConfig::getInstance()->apiUrl);
        $this->modulePluginStatus = new ModulePluginStatus();

        parent::__construct();
        $this->displayName = $this->l('Module_name reviews');
        $this->version = '#{Octopus.Release.Number}';
        $this->description = $this->l('Module description');
        $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
    }

Please let me know if there is anything else we can do to help this. 

As in, try to remove all other functionality and just register the hooks?

I did try to comment other things out, and it still hit the same error.

Thank you!

Link to comment
Share on other sites

We have now managed to see this error message;

image.png.297bf00786301a40821ef8c0dff727af.png

Our method clearly is available, so perhaps something is wrong with the visibility, or syntax? Or this is just a symptom of a deeper root cause issue.

Link to comment
Share on other sites

Here is the hookDisplayHeader function

    public function hookDisplayHeader(array $params)
    {
        Logger::addLog('hook display header', 2);
        if (!$this->active) {
            Logger::addLog('Module_name module: Skipping Module_name script rendering. Module is not active', 2);
            return;
        }

        $config = ModuleNameConfig::getInstance();
        $ModuleName = ModuleName::getInstance($this->context);
        $ModuleName_settings = $config->getFromMasterSettings('ModuleName');
        $this->context->smarty->compile_check = true;
        $this->context->smarty->assign(
            array(
                'script_url' => $config->script_url,
                'key' =>  $config->getFromMasterSettings('general')->key,
                'widget_script_url' => $config->widget_script_url,
                'preview_script_url' => $config->preview_script_url,
                'preview_css_url' => $config->preview_css_url,
                'integration_app_url' => $this->getDomainName($config->integration_app_url),
                'register_js_dir' => __ASSETS_JS_DIR__ . '/tp_register.min.js',
                'module_name_box_js_dir' => __ASSETS_JS_DIR__ . '/tp.module_name_.min.js',
                'preview_js_dir' => __ASSETS_JS_DIR__ . '/tp_preview.min.js',
                'module_name__ajax_url' => $this->context->link->getModuleLink('module_name', 'module_name_ajax'),
                'module_name__settings' => $ModuleName->loadModuleName($ModuleName_settings, $this->getLanguageId()),
                'user_id' => (int)$this->context->customer->id,
            )
        );

        return $this->context->smarty->fetch(_PS_MODULE_DIR_.'ModuleName/views/templates/hook/head.tpl');
    }

(obfuscated the module name) 

Link to comment
Share on other sites

Hola

Es posible que el hook displayHeader no exista en la tabla de hooks. PrestaShop utiliza Hook::getIdByName($hook) para comprobar si un hook existe en la base de datos. Si no devuelve un ID, indica que el hook no se ha encontrado o no está disponible en la versión de PrestaShop que estás utilizando.

Si el hook existe pero aún no se registra, podría estar relacionado con problemas de carga automática de clases. Asegúrate de que Hook::registerHook() y las clases relacionadas se carguen correctamente, y prueba a borrar la caché de PrestaShop para descartar problemas de caché.

Link to comment
Share on other sites

Hello,

Thank you again for your reply, your help is much appreciated.

I do see the hook in the db when searching through the `ps_hook` table.

image.thumb.png.c19ead7e753bd59df92698f661086092.png

I also see the `displayHeader` in the positions tab on PrestaShop as well, which I assume is a good thing.

 image.thumb.png.7e28795afe2104adf3706d869d643873.png

 

In terms of loading the Class - is there an explicit way to clear the PrestaShop cache?

However, I am seeing that any changes I make in the main module (e.g adding log lines to the code) are appearing in the PrestaShop logs correctly - implying there is no cache issue.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...