8

I want to customize Magento\Theme\Block\Html\Footer class in magento2.

how can i change some text and style of footer copyright text with custom module..

4 Answers 4

13

You don't need to create custom module. You can change the copyright text in Admin.

  1. Go to Content >> Configuration.
  2. Edit the active theme
  3. Click on Footer tab
  4. Edit the copyright Text enter image description here

OR

You can override Magento\Theme\Block\Html\Footer class in your custom module with getCopyright function like this:

<?php
namespace Vendor\Theme\Block\Html;

class Footer extends Magento\Theme\Block\Html\Footer
{
    /*
     * Retrieve copyright information
     *
     * @return string
     */
    public function getCopyright()
    {
        if (!$this->_copyright) {
            $this->_copyright = $this->_scopeConfig->getValue(
                'design/footer/copyright',
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE
            );
        }
        return __($this->_copyright);
    }

}

and update the getCopyright function as per your requirement.

If you can specify the exact requirement as to what needs to be changed I can help you in better way.

2
  • I know this method..but my concern is from Magento\Theme\Block\Html\Footer this class with custom module Aug 14, 2017 at 9:25
  • please check my answer
    – Sejal Shah
    Aug 16, 2017 at 8:53
0

If you want to customize Magento\Theme\Block\Html\Footer you need:

Create your own module.

Create YourVendor/YourModule/etc/frontend/di.xml file like:

<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="YourVendor\YourModule\Block\Html\Footer" />

Create your block like:

<?php
declare(strict_types=1);

namespace YourVendor\YourModule\Block\Html;

/**
 * Html page footer block
 */
class Footer extends \Magento\Theme\Block\Html\Footer
{
   /**
    * @inheritdoc
    */
    public function getCopyright(): string
    {
        $copyright = parent::getCopyright();

        return '| ' . $copyright . ' |';
    }
}
0
  1. Create di.xml file in <vendor_name>/<module_name>/etc/frontend (i.e Lokesh/Custom/etc/frontend/di.xml)

     <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="Lokesh\Custom\Block\Html\Footer" />
     </config>
    
  2. Create Footer.php in <vendor_name>/<module_name>/Block/Html (i.e Lokesh/Custom/Block/Html/Footer.php) and add below code

A. Without Template:

<?php
namespace Lokesh\Custom\Block\Html;

class Footer extends \Magento\Theme\Block\Html\Footer
{
     public function getCopyright()
    {
        if (!$this->_copyright) {
            $this->_copyright = "Your Copyright Content goes here";
        }
        return __($this->_copyright);
    }
}

B. With Template:

<?php
namespace Lokesh\Custom\Block\Html;

class Footer extends \Magento\Theme\Block\Html\Footer
{
     public function getCopyright()
    {
        if (!$this->_copyright) {
            $this->_copyright =  $this->getLayout()
          ->createBlock('Lokesh\Custom\Block\Html\Footer')
          ->setTemplate('Lokesh_Custom::footer.phtml')
          ->toHtml();

        }
        return __($this->_copyright);
    }

    public function getYear()
    {
        return date('Y');
    }
}

Template File: Lokesh/Custom/view/template/footer.phtml

<?php
$year = $block->getYear();
?>
<p class="copyright col-m-6">Copyright © <?= $year; ?> Company and Content</p>
   

NOTE: Execute php bin/magento cache:flush and php bin/magento setup:static-content:deploy -f in cli to see the changes on storefront

0

To Add a dynamic year in the footer copyright section use this mobile under System -> Configuration -> Design. Ability to show a year variable via the Theme Footer configuration

Here is a module used this year that will change dynamically. Even the PWA app year will change if you use this extension in Magento PWA.

https://github.com/niraligajera/magento-CopyRightFooter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.