Skip to content

Block that doesn’t explicitly declare template?

I recently followed a tutorial to add a custom fee to the checkout summary of my Magento store and after getting everything working, I realized that the block I created didn’t need to have an associated template when being added to various layout files, examples being sales_or_invoice_view and sales_guest_view, nor were the preferenced in module.xml.

I am curious why?

sales_guest_view.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="order_totals">
            <block class="VendorModuleBlockSalesOrderFee" name="fee" />
        </referenceBlock>
    </body>
</page>

Fee.php

class AdminFee extends MagentoFrameworkViewElementTemplate
{
    /**
     * @var Order
     */
    protected $_order;
    /**
     * @return Order
     */
    public function getOrder()
    {
        return $this->_order;
    }
    /**
     * Initialize all order totals relates with tax
     *
     * @return MagentoTaxBlockSalesOrderTax
     */
    public function initTotals()
    {
        $parent = $this->getParentBlock();
        $this->_order = $parent->getOrder();
        $fee = new MagentoFrameworkDataObject(
            [
                'code' => 'fee',
                'strong' => false,
                'value' => $this->_order->getFee(),
                'label' => __('Trial Fee'),
            ]
        );

        $parent->addTotal($fee, 'fee');
        return $this;
    }
}