Magento 2 : Override template file /info/default.phtml
题
How to override below file in module ?
Magento2/vendor/magento/module-payment/view/frontend/templates/info/default.phtml
解决方案
Try with below way.
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\Payment\Block\Info">
<plugin name="module_paympent_view_override_template" type="Vendor\Module\Plugin\Payment\Block\Info" />
</type>
</config>
Info.php
<?php
namespace Vendor\Module\Plugin\Payment\Block;
class Info
{
public function beforeToHtml(\Magento\Payment\Block\Info $subject)
{
$subject->setTemplate('Vendor_Module::info/default.phtml');
}
}
The above code is not tested. I have just shared the way to override.
其他提示
No, it's so easy. You need to copy that file into your vendor theme in app/desing.
So, the original path file is vendor/magento/module-payment/view/frontend/templates/info/default.phtml
, you need to copy and paste it in app/design/MyVendor/MyTheme/Magento_Payment/templates/info/default.phtml
.
After this flush magento cache.
1)Create di.xml file and add following code:
<?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="module_paympent_view_override_template" type="Vendor\Module\Payment\Block\Info" />
</config>
2)Create Block:
<?php
namespace Vendor\Module\Plugin\Payment\Block;
class Info
{
public function beforeToHtml(\Magento\Payment\Block\Info $subject)
{
$subject->setTemplate('Vendor_Module::info/default.phtml');
}
}
3)Create Template file default.phtml
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* @var \Magento\Payment\Block\Info $block
* @see \Magento\Payment\Block\Info
*/
$specificInfo = $block->getSpecificInformation();
$title = $block->escapeHtml($block->getMethod()->getTitle());
?>
<dl class="payment-method">
<dt class="title"><?= /* @noEscape */ $title ?></dt>
<?php if ($specificInfo) : ?>
<dd class="content">
<table class="data table">
<caption class="table-caption"><?= /* @noEscape */ $title ?></caption>
<?php foreach ($specificInfo as $label => $value) : ?>
<tr>
<th scope="row"><?= $block->escapeHtml($label) ?></th>
<td>
<?= /* @noEscape */ nl2br($block->escapeHtml(implode("\n", $block->getValueAsArray($value, true)))) ?>
</td>
</tr>
<?php endforeach; ?>
</table>
</dd>
<?php endif;?>
</dl>
<?= $block->getChildHtml() ?>
try to preference block Magento\Payment\Block\Info.php
file 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\Payment\Block\Info"" type=""Vendor\Module\Block\Info"" />
</config>
on file Vendor\Module\Block\Info.php
<?php
namespace Vendor\Module\Block;
class Info extends \Magento\Payment\Block\Info
{
.....................
protected $_template = 'Vendor_Module::info/default.phtml';
....................
}
Finally, create your template