Magento – How to display contents of a .phtml template file in checkout

checkoutlayoutmagento2

I've been trying to add some php logic to view the things I want. I found out that the proper way to do this using a block template in a .phtml file in the /view/frontend/templates folder of the module.

What I've tried

In my module's layout(checkout_index_index.xml) file added this:

<referenceContainer name="content">
      <block class="DeliveryMatch\Shipping\Block\DmCarrier" name="dm-shipping" template="DeliveryMatch_Shipping::checkout/shipping/carrier_custom.phtml" before="shippingAdditional" cacheable="false" />
</referenceContainer>

To display the contents of the .phtml before shippingAdditional

app/code/DeliveryMatch/Shipping/Block/DmCarrier.php

<?php
namespace DeliveryMatch\Shipping\Block\DmCarrier;

use Magento\Framework\View\Element\Template;

class DmCarrier extends \Magento\Framework\View\Element\Template
{

    public function __construct(Template\Context $context, array $data = array())
    {
        parent::__construct($context, $data);
        $this->setData('contacts',array());
    }


}

And the .phtml file in

view/frontend/templates/checkout/shipping/carrier_custom.phtml

<h1><?php echo 'Test'; ?></h1>

But this is not displayed in the checkout.

What I need

Display the contents of

templates/checkout/shipping/carrier_custom.phtml

Inside the shippingAdditional field

How do I achieve this?

Thanks in advance

Best Answer

Then you can use it

echo $this->getLayout()->createBlock("DeliveryMatch\Shipping\Block\DmCarrier\DmCarrier")->setTemplate("DeliveryMatch_Shipping::checkout/shipping/carrier_custom.phtml")->toHtml();
Related Topic