I'm trying to add a subtotal per vat rate to our PDF invoices. I've followed numerous sets of instructions (which are broadly similar) such as these:
- https://www.mageplaza.com/how-add-custom-discount-magento-2.html
- https://webkul.com/blog/set-custom-discount-fee-cart-magento-2/
- how to add fee to order totals in magento2
In my class for 7% VAT which extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal, in the collect() function, I have something like this:
public function collect(
\Magento\Quote\Model\Quote $quote,
\Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
\Magento\Quote\Model\Quote\Address\Total $total
)
{
parent::collect($quote, $shippingAssignment, $total);
$items = $shippingAssignment->getItems();
if (!count($items)) {
return $this;
}
$subTotal = $this->determineSubtotalValue($quote);
$subTotal = $this->_priceCurrency->convert($subTotal);
$total->setBaseTotalAmount('subtotal7pc', $subTotal);
$total->setSubtotal7pc($subTotal);
$total->setBaseSubtotal7pc($subTotal);
return $this;
}
I have an equivalent class for the 19% VAT subtotal.
This does work correctly on the admin order placement screen, see screenshot:

However, if I then try invoice this order and try to read the total back into the PDF invoice, I always get a value of 0.00 €
My code for this is as follows:
pdf.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/pdf_file.xsd">
<renderers>
<page type="invoice">
<renderer product_type="default">Magento\Sales\Model\Order\Pdf\Items\Invoice\DefaultInvoice</renderer>
</page>
</renderers>
<totals>
<total name="subtotal7pc">
<title translate="true">Subtotal (7% Tax)</title>
<source_field>subtotal7pc</source_field>
<model>MyCompany\InvoiceTotals\Model\Order\Pdf\Total\SubTotalByTaxRate</model>
<font_size>7</font_size>
<display_zero>true</display_zero>
<sort_order>30</sort_order>
</total>
</totals>
</config>
MyCompany\InvoiceTotals\Model\Order\Pdf\Total\SubTotalByTaxRate.php
class SubTotalByTaxRate extends \Magento\Sales\Model\Order\Pdf\Total\DefaultTotal
{
/**
* @var \Magento\Tax\Helper\Data
*/
protected $_taxHelper;
/**
* @var \Magento\Tax\Model\Calculation
*/
protected $_taxCalculation;
/**
* @var \Magento\Tax\Model\ResourceModel\Sales\Order\Tax\CollectionFactory
*/
protected $_taxOrdersFactory;
public function __construct(
\Magento\Tax\Helper\Data $taxHelper,
\Magento\Tax\Model\Calculation $taxCalculation,
\Magento\Tax\Model\ResourceModel\Sales\Order\Tax\CollectionFactory $ordersFactory,
array $data = []
) {
$this->_taxHelper = $taxHelper;
$this->_taxCalculation = $taxCalculation;
$this->_taxOrdersFactory = $ordersFactory;
parent::__construct($taxHelper,$taxCalculation,$ordersFactory,$data);
}
public function getTotalsForDisplay()
{
$amount = $this->getOrder()->formatPriceTxt($this->getAmount());
$title = __($this->getTitle());
$fontSize = $this->getFontSize() ? $this->getFontSize() : 7;
$total = ['amount' => $amount, 'label' => $title, 'font_size' => $fontSize];
return [$total];
}
public function getAmount()
{
return $this->getOrder()->getDataUsingMethod($this->getSourceField());
}
}
Here is a screenshot of the PDF invoice:
So, I think I'm nearly there, but missing something. I've tried adding the following line to my collect() function:
$total->setTotalAmount('subtotal7pc', $subTotal);
But that causes incorrect display prior to submitting the order and then has no effect on the PDF invoice.
I hope someone can help? Thank you.



sales_order_place_afterevent to record the value so you can pull it out again in the PDF invoice. – Luke Cousins Nov 29 '17 at 13:11