Adding Javascript code to specific pages using Prestashop Module

若如初见. 提交于 2019-12-08 06:27:49

问题


I'm developing a custom Prestashop Module. The requirement is simple: Add a predefined block of javascript code to specific sections of the shopping process. Those are:

  • Home page
  • Product page
  • Product added to cart
  • Purchase completed

The code will be specific to each page.

I already read the basics of module development, but I can't find documentation for this specific functionality.

I already have a working module that is installable and configurable from the back office admin. I'm assuming I need to extend the footer and check the page currently being served, but I have no idea how to do this.


回答1:


It's more simple than it appears :), you have to check in which page you are, in your hookDisplayHeader method add some if:

public function hookDisplayHeader($params){
    /* some code */

    // check if we are in homepage
    if($this->context->controller->php_self == 'index'){
        $this->context->controller->addJS('path-to-index-js');
    }

    // check if we are in product page
    if($this->context->controller->php_self == 'product'){
        $this->context->controller->addJS('path-to-product-js');
    }

    // and so on for all other pages
    /* ... */

    /* some code */
}



回答2:


Also there is a global variable in js

page_name



回答3:


Every page in PrestaShop has a unique value of page_name smarty variable.

This variable's value can be fetched through the following code in any controller:

$this->context->smarty->tpl_vars['page_name']->value

You can add a hook (i.e. hookHeader) and then add a condition in that hook for every page where you want to add the script.



来源:https://stackoverflow.com/questions/40372552/adding-javascript-code-to-specific-pages-using-prestashop-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!