Question

How to add a custom block in sales order page frontend in magento 2

enter image description here

Was it helpful?

Solution

Check the below steps for creating a custom block in Order view page.

Create a custom Module then override the layout of sales_order_view.xml

File:-Vendor/Module/view/frontend/layout/sales_order_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>
        <referenceContainer name="content">
                <block class="Vendor\Module\Block\CustomBlock" name="salescustomblock" template="Vendor_Module::custom_block.phtml" />
        </referenceContainer>
    </body>
</page>

Then Create one block file to pass the business logic to the template.

File:-Nuucleon/Viceversa/Block/CustomBlock.php

<?php 

namespace Vendor\Module\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Page\Config;

class CustomBlock extends Template
{

    public function __construct(
        Context $context,
        Config $pageConfig,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->pageConfig = $pageConfig;
    }

    public function getCustomString()
    {
        return 'Custom Block';
    }
}
?>

Create a template to show on the frontend.

File:- Nuucleon/Viceversa/view/frontend/templates/custom_block.phtml

<?php 
/**
 *@var Vendor\Magento\Block\CustomBlock $block
 **/
?>
<p>
    <?= $block->getCustomString() ?>
</p>

Output:

enter image description here

I hope this will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top