簡體   English   中英

無法將任何樣式表添加到 prestashop 1.7 中的模塊

[英]Cannot add any stylesheets to module in prestashop 1.7

我在向 prestashop 前台添加任何樣式表時遇到了問題。 我閱讀了多篇文章,嘗試了多種解決方案,但無法正常工作。 向后台添加樣式不是問題(但我認為這段用於向后台添加樣式的代碼是解決方法)。 這是模塊代碼。 (我在多個地方添加了樣式表導入以檢查每個解決方案。在其他模塊中,此方法按預期工作)。 抱歉,我的 PHP 不太好,亂七八糟的代碼。

<?php

if (!defined('_PS_VERSION_'))
    exit();

class PromotionBanner extends Module
{
    public function __construct()
    {
        $this->name = 'promotionbanner';
        $this->tab = 'front_office_features';
        $this->version = '1.0.0';
        $this->author = 'example';
        $this->author_uri = 'https://example.com';
        $this->ps_versions_compliancy = array('min' => '1.7.1.0', 'max' => _PS_VERSION_);
        $this->bootstrap = true;
        $this->need_instance = 0;
        $this->dir = '/modules/promotionbanner';
        $this->css_path = Tools::getShopDomainSsl(true, true) . __PS_BASE_URI__ . 'modules/' . $this->name
            . '/' . $this->_path . 'views/css/';

        parent::__construct();

        $this->displayName = $this->l('Promotion Banner', 'promotionbanner');
        $this->description = $this->l('This module provides configurable promotion banner on your website');

        $this->confirmUninstall = $this->l('Are you sure you want to uninstall the module?', 'promotionbanner');
    }

    private function updateConf()
    {
        Configuration::updateValue('banner_text', $this->l('Wybrane produkty tańsze o 15%! Kod rabatowy: '));
        Configuration::updateValue('banner_coupon_code', $this->l('Wybierz kupon rabatowy'));
    }

    public function install()
    {
        $this->updateConf();
        return parent::install() && $this -> registerHook('displayWrapperTop') && $this->registerHook('header');
    }

    public function uninstall()
    {
        if (!parent::uninstall() || !Configuration::deleteByName('promotionbanner_module') &&
            !Configuration::deleteByName('banner_coupon_code'))
            return false;
        return true;
    }

    public function hookDisplayWrapperTop($params)
    {
        $this->context->smarty->assign(
            array(
                'banner_text' => Configuration::get('banner_text'),
                'banner_coupon_code' => Configuration::get('banner_coupon_code')
            )
        );

        $this->context->controller->registerStylesheet(
            'modules-promotion-banner2', //This id has to be unique
            'modules/'.$this->name.'/views/css/front.css',
            array('media' => 'all', 'priority' => 150)
        );

        return $this->display(__FILE__, 'promotionbanner.tpl');
    }

    public function hookHeader() {
        $this->context->controller->addCSS($this->_path . 'views/css/front.css', 'all');
        $this->context->controller->registerStylesheet(
            'modules-promotion-banner', //This id has to be unique
            'modules/'.$this->name.'/views/css/front.css',
            array('media' => 'all', 'priority' => 150)
        );
    }

    public function hookActionFrontControllerSetMedia($params) {
        $this->context->controller->registerStylesheet(
            'module-promotionbanner-style',
            'modules/'.$this->name.'/views/css/front.css',
            [
                'media' => 'all',
                'priority' => 200,
            ]
        );
    }

    public function getPromotions()
    {
        $cart_rule = _DB_PREFIX_ . 'cart_rule';
        $request = "SELECT $cart_rule.id_cart_rule, " . _DB_PREFIX_ . "cart_rule_lang.name, $cart_rule.code " .
            "FROM $cart_rule INNER JOIN " . _DB_PREFIX_ . 'cart_rule_lang ON ' . _DB_PREFIX_ . 'cart_rule.id_cart_rule='
            . _DB_PREFIX_ . 'cart_rule_lang.id_cart_rule WHERE ' . _DB_PREFIX_ . 'cart_rule.code IS NOT NULL';
        $db = Db::getInstance();
        $cupons = $db->executeS($request);
        $parsedCupons = array();
        foreach ($cupons as $cupon) {
            array_push($parsedCupons, array(
                'code' => $cupon['code'],
                'name' => $cupon['name']
            ));
        }
        return $parsedCupons;
    }

    public function displayForm()
    {
        $form = $this->renderForm();

        $this->context->smarty->assign(array(
            'banner_text' => Configuration::get('banner_text'),
            'banner_coupon_code' => Configuration::get('banner_coupon_code'),
            'form_url' => AdminController::$currentIndex . '&configure=' . $this->name . '&token=' . Tools::getAdminTokenLite('AdminModules'),
            'name' => $this->name,
            'form_tpl' => $form,
            'coupon_codes' => $this->getPromotions()
        ));
        $this->context->controller->addCSS(array(
            $this->css_path . 'fontawesome-all.min.css',
            $this->css_path . 'module.css'
        ));

        $this->output = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/menu.tpl');

        return $this->output;
    }

    public function renderForm()
    {
        $helper = new HelperForm();

        $helper->module = $this;
        $helper->name_controller = $this->name;
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        $helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
        $helper->title = $this->displayName;
        $helper->show_toolbar = false;
        $helper->toolbar_scroll = false;
        $helper->submit_action = 'submit' . $this->name;
        $helper->toolbar_btn = array(
            'save' =>
                array(
                    'desc' => $this->l('Save'),
                    'href' => AdminController::$currentIndex . '&configure=' . $this->name . '&save' . $this->name .
                        '&token=' . Tools::getAdminTokenLite('AdminModules'),
                )
        );

        $helper->fields_value = array(
            'banner_text' => Configuration::get('banner_text'),
            'banner_coupon_code' => Configuration::get('banner_coupon_code')
        );

        return $helper->generateForm(array($this->getConfigForm()));
    }

    public function getConfigForm()
    {
        $fields_form = array(
            'form' => array(
                'input' => array(
                    array(
                        'type' => 'text',
                        'label' => $this->l('Banner text before code: '),
                        'name' => 'banner_text',
                        'lang' => false,
                        'required' => true,
                        'size' => 20
                    ),
                    array(
                        'type' => 'select',
                        'label' => $this->l('Coupon code: '),
                        'name' => 'banner_coupon_code',
                        'required' => true,
                        'options' => array(
                            'query' => $this->getPromotions(),
                            'id' => 'code',
                            'name' => 'name'
                        )
                    )
                ),
                'submit' => array(
                    'title' => $this->l('Save'),
                    'class' => 'btn btn-default pull-right'
                )
            )
        );

        return $fields_form;
    }

    public function getContent()
    {
        $output = "";

        if (Tools::isSubmit('submit' . $this->name)) {
            $banner_text = strval(Tools::getValue('banner_text'));
            $banner_coupon_code = strval(Tools::getValue('banner_coupon_code'));

            if (!isset($banner_text) || !isset($banner_coupon_code))
                $output .= $this->displayError($this->l('Please insert something in this field.'));
            else {
                Configuration::updateValue('banner_text', $banner_text);
                Configuration::updateValue('banner_coupon_code', $banner_coupon_code);
                $output .= $this->displayConfirmation($this->l('Field updated successfully!'));
            }
        }
        return $output . $this->displayForm();
    }


}

好的,我已經成功地在 prestashop 1.7.6.1中注冊了樣式表。 但我認為現在是提及我的一些錯誤並解決一些問題的好時機。

在前台注冊樣式表的清單

  1. 為工作使用正確的鈎子。 我的問題一直沒有解決,因為我用錯了鈎子。
  2. 確保您的__construct()注冊了用於注冊樣式表的官方掛鈎 (prestashop 1.7.x) 正確的鈎子是: $this->registerHook('actionFrontControllerSetMedia'); 你可以在這里找到官方文檔(如果你的模塊中沒有前端控制器): https ://devdocs.prestashop.com/1.7/themes/getting-started/asset-management/#without-a-front-controller -模塊
  3. 確保你用$params注冊了你的鈎子函數(我不知道為什么但是如果沒有定義函數參數它就不能工作......這也阻止了我成功注冊)。 正確的功能應該是這樣的:
        $this->context->controller->registerStylesheet(
            'stylesheet-id', //This id has to be unique
            'modules/'.$this->name.'/views/css/front.css',
            array('server' => 'local', 'priority' => 10)
        );
    }
  1. 正如@bakis 提到的。 每次嘗試為 chrome 或 chromium 用戶清除瀏覽器緩存 + prestashop 緩存后,我建議完全禁用檢查器窗口中的瀏覽器緩存。
  2. 我知道$this->context->controller->addCSS仍然存在,但是這個函數只對后台樣式表注冊有用。 甚至官方文檔都在談論它

addJS()、addCSS()、addJqueryUI() 和 addJqueryPlugin() 方法保持向后兼容性。

這幾乎就是這個問題的全部內容。 我希望它能幫助將來正在尋找答案的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM