Prestashop add css to a module

15,498

Solution 1

Did you remember about registering a hook for the header during module's installation?

function install() {
    if (!parent::install())
        return false;
    if (!$this->registerHook('header'))
        return false;
    return true;
}

Without it you will have to use "transplant module" function from Admin > Modules > Positions, to do this. Always check with tools like Firebug to verify whether your files are there.

Also, I think there is something missing, can you supply us with the full code of your module? Please, provide us with a Prestashop version that you use as well.

Solution 2

Other solution:

$this->context->controller->addCSS(($this->_path).'style.css', 'all'); 
$this->context->controller->addJs(($this->_path).'script.js', 'all'); 

Greetings,

source info:

http://www.prestashop.com/forums/topic/235476-solucionadoerror-en-mi-1ra-web-warning-function-addcss-is-deprecated-in/

Solution 3

Which is wierd, because $this->_path points to the module folder : modules//

yes it is (weird), but... in the addCSS function, it is overriden with (module) themes css folder

public static function addCSS($css_uri, $css_media_type = 'all')
{
  global $css_files;
   ...
  $css_uri = str_replace(__PS_BASE_URI__.'modules/', __PS_BASE_URI__.'themes/'._THEME_NAME_.'/css/modules/', $css_uri, $different);
   ...
}
Share:
15,498
Benjamin Crouzier
Author by

Benjamin Crouzier

I am a former developer (rails/react/aws/postgres), and I now study AI and cognitive science full-time. Currently living in Paris. Github profile: https://github.com/pinouchon Blog: http://pinouchon.github.io/

Updated on June 04, 2022

Comments

  • Benjamin Crouzier
    Benjamin Crouzier almost 2 years

    I am creating a module in prestashop 1.4, say blocktest

    modules/blocktest/blocktest.php:

    ...
    
    public function hookLeftColumn($params)
    {
        global $smarty;
        $smarty->assign(array(
            'test' => 'test'
        ));
        return $this->display(__FILE__, 'blocktest.tpl');
    }
    
    public function hookHeader()
    {
        Tools::addCSS($this->_path.'blocktest.css', 'all');
    }
    

    modules/blocktest/blocktest.css:

    * { background-color: red; }
    


    Problem:

    My css is not included.


    What i tried:

    In admin > preferences > performances > smarty, i have set cache to no, and force compile to yes. In admin > preferences > performances > smarty, cache is set to no.

    Existing modules uses the same css inclusion : Tools::addCSS($this->_path.'blocktest.css', 'all');, but the css file is in <themeName>/css/modules/<moduleName>/<moduleName>.css. Which is wierd, because $this->_path points to the module folder : modules/<moduleName>/.

    But anyway, I tried to put my css file in <themeName>/css/modules/blocktest/blocktest.css, that doesn't work. Maybe i am missing something