I give my answer about order email. Other emails and pdf should works with the same logic.
You have to look at \vendor\magento\module-sales\view\frontend\layout\sales_email_order_items.xml
where the block with class Magento\Sales\Block\Order\Email\Items
has the template \vendor\magento\module-sales\view\frontend\templates\order\items.phtml
In this template the line <?php echo $block->getChildHtml('order_totals') ?>
calls the block with totals (always defined in sales_email_order_items.xml
)
...
<block class="Magento\Sales\Block\Order\Totals" name="order_totals" template="order/totals.phtml">
...
This template starts with
<?php foreach ($block->getTotals() as $_code => $_total): ?>
where $block
is a Magento\Sales\Block\Order\Totals
instance.
Then, to add your custom field to totals, try to write a plugin related to one public method of this class, i think you could choose addTotal()
or addTotalBefore()
, in this way your custom field will be added to $this->_totals
array and it will be printed from the foreach loop.