Cac*_*ost 3 php prestashop prestashop-1.7
我在向 prestashop 前台添加任何样式表时遇到了问题。我阅读了多篇文章,尝试了多种解决方案,但无法使其发挥作用。将样式添加到后台不是问题(但我认为这段用于将样式添加到后台的代码是解决方法)。这是模块代码。(我在多个位置添加了样式表导入来检查每个解决方案。在其他模块中,此方法按预期工作)。抱歉,代码很乱,我不太擅长 PHP。
\n\n<?php\n\nif (!defined(\'_PS_VERSION_\'))\n exit();\n\nclass PromotionBanner extends Module\n{\n public function __construct()\n {\n $this->name = \'promotionbanner\';\n $this->tab = \'front_office_features\';\n $this->version = \'1.0.0\';\n $this->author = \'example\';\n $this->author_uri = \'https://example.com\';\n $this->ps_versions_compliancy = array(\'min\' => \'1.7.1.0\', \'max\' => _PS_VERSION_);\n $this->bootstrap = true;\n $this->need_instance = 0;\n $this->dir = \'/modules/promotionbanner\';\n $this->css_path = Tools::getShopDomainSsl(true, true) . __PS_BASE_URI__ . \'modules/\' . $this->name\n . \'/\' . $this->_path . \'views/css/\';\n\n parent::__construct();\n\n $this->displayName = $this->l(\'Promotion Banner\', \'promotionbanner\');\n $this->description = $this->l(\'This module provides configurable promotion banner on your website\');\n\n $this->confirmUninstall = $this->l(\'Are you sure you want to uninstall the module?\', \'promotionbanner\');\n }\n\n private function updateConf()\n {\n Configuration::updateValue(\'banner_text\', $this->l(\'Wybrane produkty ta\xc5\x84sze o 15%! Kod rabatowy: \'));\n Configuration::updateValue(\'banner_coupon_code\', $this->l(\'Wybierz kupon rabatowy\'));\n }\n\n public function install()\n {\n $this->updateConf();\n return parent::install() && $this -> registerHook(\'displayWrapperTop\') && $this->registerHook(\'header\');\n }\n\n public function uninstall()\n {\n if (!parent::uninstall() || !Configuration::deleteByName(\'promotionbanner_module\') &&\n !Configuration::deleteByName(\'banner_coupon_code\'))\n return false;\n return true;\n }\n\n public function hookDisplayWrapperTop($params)\n {\n $this->context->smarty->assign(\n array(\n \'banner_text\' => Configuration::get(\'banner_text\'),\n \'banner_coupon_code\' => Configuration::get(\'banner_coupon_code\')\n )\n );\n\n $this->context->controller->registerStylesheet(\n \'modules-promotion-banner2\', //This id has to be unique\n \'modules/\'.$this->name.\'/views/css/front.css\',\n array(\'media\' => \'all\', \'priority\' => 150)\n );\n\n return $this->display(__FILE__, \'promotionbanner.tpl\');\n }\n\n public function hookHeader() {\n $this->context->controller->addCSS($this->_path . \'views/css/front.css\', \'all\');\n $this->context->controller->registerStylesheet(\n \'modules-promotion-banner\', //This id has to be unique\n \'modules/\'.$this->name.\'/views/css/front.css\',\n array(\'media\' => \'all\', \'priority\' => 150)\n );\n }\n\n public function hookActionFrontControllerSetMedia($params) {\n $this->context->controller->registerStylesheet(\n \'module-promotionbanner-style\',\n \'modules/\'.$this->name.\'/views/css/front.css\',\n [\n \'media\' => \'all\',\n \'priority\' => 200,\n ]\n );\n }\n\n public function getPromotions()\n {\n $cart_rule = _DB_PREFIX_ . \'cart_rule\';\n $request = "SELECT $cart_rule.id_cart_rule, " . _DB_PREFIX_ . "cart_rule_lang.name, $cart_rule.code " .\n "FROM $cart_rule INNER JOIN " . _DB_PREFIX_ . \'cart_rule_lang ON \' . _DB_PREFIX_ . \'cart_rule.id_cart_rule=\'\n . _DB_PREFIX_ . \'cart_rule_lang.id_cart_rule WHERE \' . _DB_PREFIX_ . \'cart_rule.code IS NOT NULL\';\n $db = Db::getInstance();\n $cupons = $db->executeS($request);\n $parsedCupons = array();\n foreach ($cupons as $cupon) {\n array_push($parsedCupons, array(\n \'code\' => $cupon[\'code\'],\n \'name\' => $cupon[\'name\']\n ));\n }\n return $parsedCupons;\n }\n\n public function displayForm()\n {\n $form = $this->renderForm();\n\n $this->context->smarty->assign(array(\n \'banner_text\' => Configuration::get(\'banner_text\'),\n \'banner_coupon_code\' => Configuration::get(\'banner_coupon_code\'),\n \'form_url\' => AdminController::$currentIndex . \'&configure=\' . $this->name . \'&token=\' . Tools::getAdminTokenLite(\'AdminModules\'),\n \'name\' => $this->name,\n \'form_tpl\' => $form,\n \'coupon_codes\' => $this->getPromotions()\n ));\n $this->context->controller->addCSS(array(\n $this->css_path . \'fontawesome-all.min.css\',\n $this->css_path . \'module.css\'\n ));\n\n $this->output = $this->context->smarty->fetch($this->local_path . \'views/templates/admin/menu.tpl\');\n\n return $this->output;\n }\n\n public function renderForm()\n {\n $helper = new HelperForm();\n\n $helper->module = $this;\n $helper->name_controller = $this->name;\n $helper->token = Tools::getAdminTokenLite(\'AdminModules\');\n $helper->currentIndex = AdminController::$currentIndex . \'&configure=\' . $this->name;\n $helper->title = $this->displayName;\n $helper->show_toolbar = false;\n $helper->toolbar_scroll = false;\n $helper->submit_action = \'submit\' . $this->name;\n $helper->toolbar_btn = array(\n \'save\' =>\n array(\n \'desc\' => $this->l(\'Save\'),\n \'href\' => AdminController::$currentIndex . \'&configure=\' . $this->name . \'&save\' . $this->name .\n \'&token=\' . Tools::getAdminTokenLite(\'AdminModules\'),\n )\n );\n\n $helper->fields_value = array(\n \'banner_text\' => Configuration::get(\'banner_text\'),\n \'banner_coupon_code\' => Configuration::get(\'banner_coupon_code\')\n );\n\n return $helper->generateForm(array($this->getConfigForm()));\n }\n\n public function getConfigForm()\n {\n $fields_form = array(\n \'form\' => array(\n \'input\' => array(\n array(\n \'type\' => \'text\',\n \'label\' => $this->l(\'Banner text before code: \'),\n \'name\' => \'banner_text\',\n \'lang\' => false,\n \'required\' => true,\n \'size\' => 20\n ),\n array(\n \'type\' => \'select\',\n \'label\' => $this->l(\'Coupon code: \'),\n \'name\' => \'banner_coupon_code\',\n \'required\' => true,\n \'options\' => array(\n \'query\' => $this->getPromotions(),\n \'id\' => \'code\',\n \'name\' => \'name\'\n )\n )\n ),\n \'submit\' => array(\n \'title\' => $this->l(\'Save\'),\n \'class\' => \'btn btn-default pull-right\'\n )\n )\n );\n\n return $fields_form;\n }\n\n public function getContent()\n {\n $output = "";\n\n if (Tools::isSubmit(\'submit\' . $this->name)) {\n $banner_text = strval(Tools::getValue(\'banner_text\'));\n $banner_coupon_code = strval(Tools::getValue(\'banner_coupon_code\'));\n\n if (!isset($banner_text) || !isset($banner_coupon_code))\n $output .= $this->displayError($this->l(\'Please insert something in this field.\'));\n else {\n Configuration::updateValue(\'banner_text\', $banner_text);\n Configuration::updateValue(\'banner_coupon_code\', $banner_coupon_code);\n $output .= $this->displayConfirmation($this->l(\'Field updated successfully!\'));\n }\n }\n return $output . $this->displayForm();\n }\n\n\n}\nRun Code Online (Sandbox Code Playgroud)\n
好的,我已经成功在 prestashop 1.7.6.1中注册了样式表。但我认为现在是提及我的一些错误并解决一些问题的好时机。
在前台注册样式表的清单
__construct()注册了用于注册样式表的官方挂钩(prestashop 1.7.x)。正确的钩子是:$this->registerHook('actionFrontControllerSetMedia');您可以在这里找到官方文档(如果您的模块中没有前端控制器): https: //devdocs.prestashop.com/1.7/themes/getting-started/asset-management/#without-前端控制器模块$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)
);
}
Run Code Online (Sandbox Code Playgroud)
$this->context->controller->addCSS仍然存在,但此功能仅对后台样式表注册有用。连官方文档都这么说addJS()、addCSS()、addJqueryUI() 和 addJqueryPlugin() 方法保持向后兼容性。
这几乎就是这个问题的全部内容。我希望它能帮助将来正在寻找答案的人。
| 归档时间: |
|
| 查看次数: |
1560 次 |
| 最近记录: |