6

Default Magento payment method "Bank Tranfer" does not allow using HTML tags in Instructions text.

How can I change that?

1
  • Can you please give graphical detail what you actually want in your question ? Apr 5, 2019 at 12:02

1 Answer 1

9
+50

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:

Admin enter image description here

Front Admin

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; ?>
6
  • This is working fine for the frontend. I have displayed an image on the frontend but in the backend, only the image URL is showing. Oct 14, 2020 at 6:51
  • @VinodKumar Same prblm at my end. Did u got any solution for this?
    – Nafsss
    Mar 22, 2021 at 8:09
  • @Nafsss please see my Update I in answer
    – Pawan
    Mar 25, 2021 at 10:44
  • @VinodKumar please see my Update I in answer..
    – Pawan
    Mar 25, 2021 at 10:44
  • +1 worked...thanx Sep 17, 2021 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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