系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》
本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。
以编程方式在Magento 2发票电子邮件中的发票总计中添加自定义字段的步骤:
步骤1:第一步,您需要 在以下文件路径中创建sales_order_invoice.xml文件
app/code/Vendor/Extension/view/frontend/layout/sales_order_invoice.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> <referenceBlock name="invoice_totals"> <block class="Vendor\Extension\Block\CustomFieldinInvoice" name="invoice"/> <action method="setLabel"> <argument name="label" xsi:type="string">Custom Field Name</argument> </action> </referenceBlock> </body> </page>
步骤2:现在在 以下路径创建自定义字段发票.php文件
app/code/Vendor/Extension/Block/CustomFieldinInvoice.php
现在添加以下代码
<?php
namespace Vendor\Extension\Block;
use Magento\Framework\View\Element\Template;
class CustomFieldinInvoice extends Template
{
public function __construct(
Template\Context $context,
array $data = []
)
{
parent::__construct($context, $data);
$this->setInitialFields();
}
public function setInitialFields()
{
if (!$this->getLabel())
{
$this->setLabel(__('Custom Field Name'));
}
}
public function initTotals()
{
$this->getParentBlock()->addTotal(
new \Magento\Framework\DataObject(
[
'code' => 'customfield',
'strong' => $this->getStrong(),
'value' => $this->getOrder()->getCustom(), // extension attribute field
'base_value' => $this->getOrder()->getCustom(),
'label' => __($this->getLabel()),
]
),
$this->getAfter()
);
return $this;
}
public function getOrder()
{
return $this->getParentBlock()->getOrder();
}
public function getSource()
{
return $this->getParentBlock()->getSource();
}
}结论:
因此,通过这种方式,您可以轻松地以编程方式在Magento 2发票电子邮件中的发票总计中添加自定义字段。