Call custom phtml on success page without override
You can use any of Magento's layout file in your module to insert your custom block. In your case you need to use checkout_onepage_success.xml
layout file in your custom module which will be active when a customer is on order success page. In the layout file you need to specify where you want to add your custom template using referenceContainer
or referenceBlock
.
For you I've created a simple module and tested it which works fine.
registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
Magento\Framework\Component\ComponentRegistrar::MODULE,
'Amit_Orderpage',
__DIR__
);
composer.json
{
"name": "amit/orderpage-module",
"description": "Simple Magento 2 module that adds a new template on order success page.",
"type": "magento2-module",
"version": "1.0.0",
"license": [
"OSL-3.0",
"AFL-3.0"
],
"require": {
"php": "~5.5.0|~5.6.0|~7.0.0",
"magento/framework": "~100.0"
},
"autoload": {
"files": [ "registration.php" ],
"psr-4": {
"Amit\\Orderpage\\": ""
}
}
}
etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Amit_Orderpage" setup_version="2.0.0"/>
</config>
Block/Success.php
<?php
namespace Amit\Orderpage\Block;
class Success extends \Magento\Framework\View\Element\Template
{
public function getSomething()
{
return 'returned something from custom block.';
}
}
view/frontend/layout/checkout_onepage_success.xml
<?xml version="1.0"?>
<body>
<referenceContainer name="order.success.additional.info">
<block class="Amit\Orderpage\Block\Success"
name="amit.order.success"
template="Amit_Orderpage::order/success.phtml"
after="-">
</block>
</referenceContainer>
</body>
Specify before
or after
tag to specify where you want to add your template before or after order.success.additional.info
container. Here after="-"
will add your your template to order.success.additional.info
container after all other containers present in the same section.
view/frontend/templates/order/success.phtml
<?php /* @var $block \Amit\Orderpage\Block\Success */?>
<?php echo __('Custom template file content.'); ?>
<?php echo $block->getSomething(); ?>
If you would like to utilize/access the current $order object in your block/html file, without overriding original order success block/template. Here is the complete solution:
registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'MageAj_OrderSuccess',
__DIR__
);
etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MageAj_OrderSuccess" setup_version="2.0.0" />
</config>
app/code/MageAj/OrderSuccess/view/frontend/layout/checkout_onepage_success.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="order.success.additional.info">
<block class="Magento\Framework\View\Element\Template" name="mageaj.success" template="MageAj_OrderSuccess::checkout/onepage/success.phtml" cacheable="false">
<arguments>
<argument name="view_model" xsi:type="object">MageAj\OrderSuccess\ViewModel\Checkout\Onepage\Success</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
app/code/MageAj/OrderSuccess/ViewModel/Checkout/Onepage/Success.php
<?php
namespace MageAj\OrderSuccess\ViewModel\Checkout\Onepage;
class Success implements \Magento\Framework\View\Element\Block\ArgumentInterface
{
protected $_checkoutSession;
public function __construct(
\Magento\Checkout\Model\Session $checkoutSession
) {
$this->_checkoutSession = $checkoutSession;
}
public function getOrder()
{
return $this->_checkoutSession->getLastRealOrder();
}
}
app/code/MageAj/OrderSuccess/view/frontend/templates/order/success.phtml
<?php $order = $block->getViewModel()->getOrder(); ?>
If you dont want to create new module, you can do it in your theme as well. Follow below steps:
1.Create checkout_onepage_success.xml
in your theme. For example:
app/design/frontend///Magento_Checkout/layout/checkout_onepage_success.xml.
The content of file is:
<?xml version="1.0"?>
<body>
<referenceContainer name="order.success.additional.info">
<block class="Magento\Checkout\Block\Onepage\Success"
name="custom_info.order.success"
template="Magento_Checkout::order/success/custom_info.phtml"
after="-">
</block>
</referenceContainer>
</body>
2.Then you need to create the template file:
app/design/frontend///Magento_Checkout/templates/order/success/custom_info.phtml
You can add any content in the template. For example:
<?php /* @var $block \Magento\Checkout\Block\Onepage\Success */?>
<?php echo __('Custom template file content.'); ?>
<?php echo $block->getSomething(); ?>
Clear the cache and you are done.