1

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.

  • did you get any error? – fmsthird Apr 3 at 5:38
  • No errors, it wouldn't call any methods from the custom block and also doesn't display OOB order number and other information, all that section is blank on the success page with no errors. – cnu Apr 3 at 12:28
1

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.

  • I have made layout xml changes as above butt still not working. I may have mixed it up with changes from both answers, I am not sure if below custom module di.xml, and module.xml are necessary since the custom template should get data from the new Block and display after OOB success page. Am I right? di.xml: <preference for="Magento\Checkout\Block\Onepage\Success" type="Vendor\Checkout\Block\Onepage\CustomSuccess"/> module.xml: <module name="Vendor_Checkout" setup_version="2.0.0"> <sequence> <module name="Magento_Checkout"/> </sequence> </module> – cnu Apr 3 at 15:46
  • I removed above mentioned entries from di.xml and module.xml and I still don't see custom phtml code being rendered though the module is enabled, ran upgrade and deployed static assets, cleared cache as well couple of times. Not sure what I am missing now. – cnu Apr 3 at 16:05
  • At the end it was blank config entry in di.xml since layout entry already is referencing new Block and template phtml file as below. Other layout entry may have also worked but now left it with following and it's working now. <referenceContainer name="order.success.additional.info"> <block class="VendoName\Checkout\Block\Onepage\CustomSuccess" name="custom.order.success" template="VendoName_Checkout::checkout/mysuccess.phtml" after="-"> </block> </referenceContainer> – cnu Apr 3 at 17:27
  • My bad, there was a lower case letter in the vendor name inside checkout_onepage_success.xml while referencing the custom template, hence it wasn't called. I saw this issue in Developer mode but didn't enable it again since I thought I had fixed. It came up again since I was trying multiple things – cnu Apr 3 at 17:30
3

You can follow below file path to get additional order information to display order success page like order total.

File path: magento/app/code/Vendor/OrderSuccess/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_OrderSuccess',
    __DIR__
);

File path: magento/app/code/Vendor/OrderSuccess/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="Vendor_OrderSuccess" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Checkout"/>
        </sequence>
    </module>
</config>

File path: magento/app/code/Vendor/OrderSuccess/etc/frontend/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Block\Onepage\Success" type="Vendor\OrderSuccess\Block\Onepage\CustomSuccess" />
</config>

File path: magento/app/code/Vendor/OrderSuccess/Block/Onepage/CustomSuccess.php

<?php
namespace Vendor\OrderSuccess\Block\Onepage;

class CustomSuccess extends \Magento\Checkout\Block\Onepage\Success
{

    protected $orderItemsDetails;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Sales\Model\Order\Config $orderConfig,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Sales\Model\Order $orderItemsDetails,
        array $data = []
    ) {
        parent::__construct($context, $checkoutSession, $orderConfig, $httpContext, $data);
        $this->orderItemsDetails = $orderItemsDetails;
    }

    public function getOrderItemsDetails()
    {
        $IncrementId  = $this->_checkoutSession->getLastRealOrder()->getIncrementId();
        $order = $this->orderItemsDetails->loadByIncrementId($IncrementId);
       return $order;
    }
}

File path: magento/app/code/Vendor/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="content">
            <block class="Vendor\OrderSuccess\Block\Onepage\CustomSuccess" name="checkout.successcustom" template="Vendor_OrderSuccess::mysuccess.phtml">
            </block>
        </referenceContainer>
    </body>
</page>

File path: magento/app/code/Vendor/OrderSuccess/view/frontend/templates/mysuccess.phtml

<?php $_order = $block->getOrderItemsDetails(); ?>

<?php echo "Sub Total"; ?><?php echo $_order->formatPrice($_order->getSubTotal()); ?>
<?php echo "Grand Total"; ?> <?php echo $_order->formatPrice($_order->getGrandTotal()); ?>

Let me know if any query.

Hope it help!

  • @cnu your commented xml code only work for default template. – Kirti Nariya Apr 3 at 13:41
  • Thanks Kirti. I see below entry in OOB checkout_onepage_success.xml, and I don't see anything printed or called from the customsuccess.phtml, is there anything else that needs to be done to use custom template code after OOB template data? – cnu Apr 3 at 13:54
  • <referenceContainer name="content"> <block class="Magento\Checkout\Block\Onepage\Success" name="checkout.success" template="Magento_Checkout::success.phtml" cacheable="false"> <container name="order.success.additional.info" label="Order Success Additional Info"/> </block> <block class="Magento\Checkout\Block\Registration" name="checkout.registration" template="Magento_Checkout::registration.phtml" cacheable="false"/> </referenceContainer> – cnu Apr 3 at 13:55

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.