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..
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityYou don't need to create custom module. You can change the copyright text in Admin.
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.
Magento\Theme\Block\Html\Footer
this class with custom module
Aug 14, 2017 at 9:25
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 . ' |';
}
}
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>
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
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.