如何覆盖magento 2中的页脚版权文本?

时间:2017-08-14 08:17:29

标签: php magento magento2 magento2.0.2

我想使用自定义模块自定义Magento\Theme\Block\Html\Footer class

输出: Hello World!

di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Theme\Block\Html\Footer">
        <plugin name="footer-text-override" type="Hello\Test\Plugin\Footer" sortOrder="15" />
    </type>
</config>

Footer.php

<?php

namespace Hello\Test\Plugin;

use Magento\Framework\View\Element\Template;

class Footer extends \Magento\Theme\Block\Html\Footer
{
    public function getCopyright()
    {
        echo "Hello World!";
    }
}

但它不起作用..

2 个答案:

答案 0 :(得分:1)

为什么要覆盖一个类只是为了更改文本? Magento提供了一个更改页脚文本的功能。

  

转到:管理员&gt;内容&gt;设计&gt;构造

单击商店视图的编辑操作。现在,向下滚动页面,然后有页脚部分,展开它并在版权字段中输入您的文本。

保存并刷新缓存。

enter image description here

enter image description here

答案 1 :(得分:0)

要覆盖magento2中的页脚版权文本,您可以使用首选项而不是插件

所以你的di.xml看起来如下。

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Theme\Block\Html\Footer" type="Hello\Test\Plugin\Footer" />
</config>

首选项用于覆盖类。它类似于magento1中的类重写。

插件允许我们在类中的任何公共方法之前,之后和周围执行我们的代码。 (http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html