Jump to content

How can I "addJS" only to product pages?


Recommended Posts

Hi,

I'm creating a module that needs to include certain script at the product page. I'm using the `hookDisplayFooterProduct` for that:

  public function hookDisplayFooterProduct()
  {
      $this->context->smarty->assign(
          array(
              'some_key' => Configuration::get('SOME_KEY'),
          )
      );
      $this->context->controller->addJS($this->_path.'view/js/something.js');
      return $this->display(__FILE__, 'html.tpl');
  }
What I'm trying to do is to include an html snippet at the 'displayFooterProduct' and to add a script to the bottom of the page. The html part works but the script is not included.

What I'm doing wrong? 
Thanks, 
 

Pedro

Link to comment
Share on other sites

Thanks @NemoPS,
I added that code to my hookDisplayHeader and it works (I know because I display something with var_dump) but I'm still unable to include include the script with

 $this->context->controller->registerJavascript('remote-mymodule-js', 'https://.....', ['position' => 'bottom', 'priority' => 150]);


I'm using prestashop 1.7. Is there any public API documentation for methods like registerJavascript?
 

Link to comment
Share on other sites

The function is actually  $this->context->controller->addJS, I have never used that registerJavascript one honestly. I would recommend using a separate js instead of assigning it from php anyway.
If you really need to, use addJSDef();

Link to comment
Share on other sites

I also tried with addJS without any success.
 

public function hookDisplayHeader()
{
    $this->context->controller->addJS($this->_path.'view/js/something.js');
    $this->context->smarty->assign(
        array(
            'my_module_key' => Configuration::get('my_module_key')
        )
    );
    return $this->display(__FILE__, 'test.tpl');
}

I expect to see something.js being included on the page. I'd disabled all caches, hit reset on the module. As a workaround I fixed my problem by adding a <script> tag with smarty.
 

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...