Allow HTML tags in Bank Tranfer Payment instructions
-
15-04-2021 - |
Domanda
Default Magento payment method "Bank Tranfer" does not allow using HTML tags in Instructions text.
How can I change that?
Soluzione
As I understood you want to add some HTML tags to Bank transfer Payment method.
By default Magento escapeHtml
when rendering Instruction for COD
and Bank transfer Payment
.
We need to Create a small module which will override getInstructions
function.
let's create a module Pawan_Instructions
.
app/code/Pawan/Instructions/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Pawan_Instructions',
__DIR__
);
app/code/Pawan/Instructions/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Pawan_Instructions" setup_version="1.0.0">
</module>
</config>
app/code/Pawan/Instructions/etc/frontend/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\OfflinePayments\Model\InstructionsConfigProvider" type="Pawan\Instructions\Model\Rewrite\InstructionsConfigProvider" />
</config>
app/code/Pawan/Instructions/Model/Rewrite/InstructionsConfigProvider.php
<?php
namespace Pawan\Instructions\Model\Rewrite;
class InstructionsConfigProvider extends \Magento\OfflinePayments\Model\InstructionsConfigProvider
{
public function getInstructions($code)
{
if($code == 'banktransfer'){//check payment method is banktransfer
return nl2br($this->methods[$code]->getInstructions());// removed escapeHtml function!
}else{
return nl2br($this->escaper->escapeHtml($this->methods[$code]->getInstructions()));
}
}
}
Final Result will be:
Note:
I have checked and allow html tag for only banktransfer
code, if you want for both, you can skip If condition.
Update I
Above code will only work for Front-end because for admin there is separate phtml
file is responsible for showing payment method instruction.
If You also want to remove escapeHtml
for Admin, follow below instruction:
Create di.xml
(for override template) at:
app/code/Pawan/Instructions/etc/adminhtml/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\OfflinePayments\Block\Form\Banktransfer">
<plugin name="override_template" type="Pawan\Instructions\Plugin\OfflinePayments\Block\Form\Bank" />
</type>
</config>
Create Bank.php
at :
app/code/Pawan/Instructions/Plugin/OfflinePayments/Block/Form/Bank.php
<?php
namespace Pawan\Instructions\Plugin\OfflinePayments\Block\Form;
class Bank
{
public function beforeToHtml(\Magento\OfflinePayments\Block\Form\Banktransfer $subject)
{
$subject->setTemplate('Pawan_Instructions::banktransfer.phtml');
}
}
create banktransfer.phtml
at:
app/code/Pawan/Instructions/view/adminhtml/templates/banktransfer.phtml
<?php
$instructions = $block->getInstructions();
?>
<?php if ($instructions) : ?>
<?php $methodCode = $block->escapeHtml($block->getMethodCode());?>
<ul class="form-list checkout-agreements" id="payment_form_<?= /* @noEscape */ $methodCode ?>" style="display:none;">
<li>
<div class="<?= /* @noEscape */ $methodCode ?>-instructions-content checkout-agreement-item-content">
<?= /* @noEscape */ $instructions ?>
</div>
</li>
</ul>
<?php endif; ?>