magento2我想添加一个块,显示每个页面上欧元和美元之间的当前汇率



小部件或其他我应该使用?当我读一些课程,小部件像静态块,如何在块中获得实时汇率

创建自定义模块在app/code/My/Module/view/frontend/layout/default.xml中创建一个文件代码:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="before.body.end">
<block class="MyModuleBlockExchangeRate" name="mymodule.exchangerate"  template="My_Module::exchange_rate.phtml" after="-"/>
</referenceContainer>
</body>
</page>

现在创建模板文件,导出html结构和数据app/代码/我/模块/视图/前端/模板/exchange_rate.phtml

<?php
/**
@var $this MyModuleBlockExchangeRate
*/
$rate =   $this->getRate();
?>
<div class="mymodule-exchange-rate>
<div>The exchange rate between euros and dollar are <?= $rate; ?></div>
</div>

最后一步为将数据传递给php创建文件应用程序/代码/我/模块/块/ExchangeRate.php

<?php
namespace MyModuleBlock;
use MagentoFrameworkViewElementTemplate;
class ExchangeRate extends Template
{
public function __construct(TemplateContext $context, array $data = [])
{
parent::__construct($context, $data);
}
public function getRate()
{
$MyRate = "";//Do stuff for get the exchange rate
return $MyRate
}
}

最新更新