How can I hook a module to a new page in prestashop?

15,784

Solution 1

To hook a module to a new page you can make follow this steps :

In my example the hook was named topSearch

Create a new hook

INSERT INTO `ps_hook` (`id_hook`, `name`, `title`, `description`, `position`) VALUES ("", "topSearch", "Top search block", "Description top search block", "1")

Allow your module to link with this new hook

You simply need to edit your module.php to add a function named with the hook, example :

public function hookTopSearch($params){
    global $smarty;
    $smarty->assign('test', 'it works !');
    return $this->display(__FILE__, 'viewfile.tpl');
}

Then link corresponded module to your new hook

Backoffice -> Module -> Position -> Grafting a module

Execute your hook in the .tpl file

You have create a new page.php, it contain include(dirname(_FILE_).'/header.php'); so override your FrontController.php by creating a new file named FrontController.php in override/classes/.

Then add your hook to the displayHeader function

<?php
  class FrontController extends FrontControllerCore{
    public function displayHeader(){
    global $css_files, $js_files;

    if (!self::$initialized)
        $this->init();

    // P3P Policies (http://www.w3.org/TR/2002/REC-P3P-20020416/#compact_policies)
    header('P3P: CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA"');

    /* Hooks are volontary out the initialize array (need those variables already assigned) */
    self::$smarty->assign(array(
        'time' => time(),
        'img_update_time' => Configuration::get('PS_IMG_UPDATE_TIME'),
        'static_token' => Tools::getToken(false),
        'token' => Tools::getToken(),
        'logo_image_width' => Configuration::get('SHOP_LOGO_WIDTH'),
        'logo_image_height' => Configuration::get('SHOP_LOGO_HEIGHT'),
        'priceDisplayPrecision' => _PS_PRICE_DISPLAY_PRECISION_,
        'content_only' => (int)Tools::getValue('content_only'),
        'exclude_page' => array('category','manufacturer')
    ));
    self::$smarty->assign(array(
        'HOOK_HEADER' => Module::hookExec('header'),
        'HOOK_TOP' => Module::hookExec('top'),
        'HOOK_TOP_SEARCH' => Module::hookExec('topSearch'),
        'HOOK_BUTTON_BRAND' => Module::hookExec('buttonBrand'),
        'HOOK_LEFT_COLUMN' => Module::hookExec('leftColumn')
    ));

    if ((Configuration::get('PS_CSS_THEME_CACHE') OR Configuration::get('PS_JS_THEME_CACHE')) AND is_writable(_PS_THEME_DIR_.'cache'))
    {
        // CSS compressor management
        if (Configuration::get('PS_CSS_THEME_CACHE'))
            Tools::cccCss();

        //JS compressor management
        if (Configuration::get('PS_JS_THEME_CACHE'))
            Tools::cccJs();
    }

    self::$smarty->assign('css_files', $css_files);
    self::$smarty->assign('js_files', array_unique($js_files));
    self::$smarty->display(_PS_THEME_DIR_.'header.tpl');
    }
  }

To finish add the corresponded smarty var to your template file

{$HOOK_TOP_SEARCH}

Solution 2

You can read here that how you can hook a module at some place and also how you can create a new hook. Please note that the module should be installed for that particular hook.

http://www.programmingtunes.com/creating-new-prestashop-hook/

Share:
15,784

Related videos on Youtube

aurel
Author by

aurel

I love JavaScript, reading books, drinking coffee and taking notes.

Updated on June 07, 2022

Comments

  • aurel
    aurel almost 2 years

    I am try to have a static page as the home page in prestashop. The only way I can think of dong this is by creating a new page (i.e. shop.php) then hook the models that are currently hooked in the home page to the new shop.php.

    I tried to follow this http://alvinjiang.blogspot.com/2011/01/prestashop-tips-how-to-create-complete.html to create a new page, it does work up to the point of displaying static text i.e. "hello world" - however I don't know to hook the homefeatured module (for example) to it.

    Can you help in any way?

    Thanks

  • aurel
    aurel over 12 years
    thanks, I will do that a bit later when I get the time but just a quick question: will I be able to add {$HOOK_TOP_SEARCH} to be page I created using the tutorial I linked above?
  • Awea
    Awea over 12 years
    Yes normally, because you include header.php and header.php call the function displayHeader() so your can use your hook every where you want :)