Extend Magento 2 Onepage Success Block to Display Additional Order Information

magento2

I created a new module to display order total and some other information on Magento 2 success since page OOB success displays order id alone. Steps taken are below.

Created new Block that extends Onepage Success and added new method(s).
Copied OOB Onepage success.phtml to custom module view\frontend\templates\mysuccess.phtml and modified to call new methods from custom Block.

Added di.xml to replace OOB <preference for="Magento\Checkout\Block\Onepage\Success" type="VendorName\Checkout\Block\Onepage\CustomSuccess"/>

Also created view\frontend\layout\checkout_onepage_success.xml with below entry to use new template phtml file.

<referenceBlock name="checkout.success" template="VendorName_Checkout::mysuccess.phtml"/>

Also created module.xml and increased sequence number. Can anyone tell me what else do I need to use the new template and Block since I see blank success page instead of new block methods getting called?

I looked at Call custom phtml on success page without override and made similar changes but still no luck. What am I missing?
It's Magento 2.2.x CE.

Best Answer

You can insert your own custom block by overriding checkout_onepage_success.xml
Try the below steps:

  1. Create your CustomSuccess.php Block file in vendor\module\Block\OnePage
<?php
namespace vendor\module\Block\OnePage;
class CustomSuccess extends \Magento\Framework\View\Element\Template
{
    public function getCustomSuccess()
    {
        return 'Your custom block contents.';
    }
} 
  1. Create checkout_onepage_success.xml layout file in vendor\module\view\frontend\layout
<?xml version="1.0"?>
<body>
    <referenceContainer name="order.success.additional.info">
        <block class="vendor\module\Block\OnePage\CustomSuccess"
               name="custom.order.success"
               template="Vendor_Module::order/success.phtml"
               after="-">
        </block>
    </referenceContainer>
</body>  
  1. Lastly, create phtml template file in vendor\module\view\frontend\templates\order
<?php /* @var $block \vendor\module\Block\OnePage\CustomSuccess */?>
<?php echo __('This is a custom content.'); ?>
<?php echo $block->getCustomSuccess(); ?>  

Don't forget to run the upgrade command and redeploy static view files.

Related Topic