2

In module-checkout, there are 2 blocks as below. I am working to show a custom success page so which one we have to override? what is the correct way?

Magento\Checkout\Block\Onepage\Success
and 
Magento\Checkout\Block\Success

Is there any option that the success.phtml file call from custom module instead of the repective theme (checkout_onepage_success.xml)?

1 Answer 1

4

Override block

Create the file app/code/Vendor/Module/etc/di.xml and add the following:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Block\Onepage\Success" type="Vendor\Module\Block\Success"/>
</config>

Create the file app/code/Vendor/Module/Block/Success.php and add the following:

<?php

namespace Vendor\Module\Block;

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

    public function getOrder() {
        return $this->_checkoutSession->getLastRealOrder();
    }

}

Override template

Create the file app/code/Vendor/Module/view/frontend/layout/checkout_onepage_success.xml and add the following:

<?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>
        <referenceBlock name="checkout.success" template="Your_Module::checkout/success.phtml"/>
    </body>
</page>

Create the file app/code/Vendor/Module/view/frontend/templates/checkout/success.phtml and add the following:

<?php /** @var $block \Vendor\Module\Block\Success */ ?>
<div class="checkout-success">
    <?php if ($block->getOrderId()):?>
        <?php if ($block->getCanViewOrder()) :?>
            <p><?php echo __('Your order number is: %1.', sprintf('<a href="%s" class="order-number"><strong>%s</strong></a>', $block->escapeHtml($block->getViewOrderUrl()), $block->escapeHtml($block->getOrderId()))) ?></p>
        <?php else: ?>
            <p><?php echo __('Your order # is: <span>%1</span>.', $block->escapeHtml($block->getOrderId())) ?></p>
        <?php endif; ?>
        
        <!-- BEGIN VENDOR_MODULE CUSTOM -->
        <p><?php echo __('You ordered %1 items.', (int) $block->getOrder()->getTotalQtyOrdered()) ?></p>
        <!-- END VENDOR_MODULE CUSTOM -->

        <p><?php /* @escapeNotVerified */ echo __('We\'ll email you an order confirmation with details and tracking info.') ?></p>
    <?php endif; ?>

    <?php echo $block->getAdditionalInfoHtml() ?>

    <div class="actions-toolbar">
        <div class="primary">
            <a class="action primary continue" href="<?php /* @escapeNotVerified */ echo $block->getUrl() ?>"><span><?php /* @escapeNotVerified */ echo __('Continue Shopping') ?></span></a>
        </div>
    </div>
</div>

That's about it, hope I could be of any help.

Edit

You probably want to refresh your checkout/success page a lot, so to tackle that problem, go to file app/code/Magento/Checkout/Controller/Onepage/Success.phtml and change

$session->clearQuote();

to

// $session->clearQuote();

This way, your quote won't get cleared when you open the page.

Updated At :- 13th Sep, 2022

Alternative Method (optional)

we can also override the template as like below one. Note that I used Magento 2.4 version. I doesn't tested with below Magento 2.4 versions.

Vendor/Module/view/frontend/layout/checkout_onepage_success.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>
        <referenceBlock name="checkout.success">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Vendor_Module::checkout/success.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>
2
  • I have already checked the stackoverflow.com/questions/34921568/… Commented May 15, 2018 at 11:01
  • @KishanPatadia thanks for the very detailed working code also I appreciate the explanation of code in your answer. It is strange that it is not accepted as an answer.
    – Sarvagya
    Commented Aug 10, 2018 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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