0

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

0

Then you can use it

echo $this->getLayout()->createBlock("DeliveryMatch\Shipping\Block\DmCarrier\DmCarrier")->setTemplate("DeliveryMatch_Shipping::checkout/shipping/carrier_custom.phtml")->toHtml();
| improve this answer | |
  • In what file can I use this? And where to setup the file so it's used by the checkout? – Dion Jun 19 '18 at 12:09
-1

You can insert your block into other template file '.phtml' of checkout.

<div style="" class="col-md-12 with-padleft">
<?php 
    echo $this->getLayout()
               ->createBlock("DeliveryMatch\Shipping\Block\DmCarrier\DmCarrier")
               ->toHtml(); 
?>
</div>
| improve this answer | |
  • In what file is this? Can you include the file path? – Dion Jun 19 '18 at 12:08
  • For example. if you want to add your block in 'module-checkout/view/frontend/templates/onepage.phtml' you need to copy this file into your module ''DeliveryMatch' that look like 'DeliveryMatch/view/frontend/templates/onepage.phtml' – Bong Channarith Jun 20 '18 at 2:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.