How get order preview on success page Magento 2

1.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 -->

TIP

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.php and change at line 22.

$session->clearQuote();
to

// $session->clearQuote();
This way, your quote won’t get cleared when you open the page.

Leave a comment

Your email address will not be published. Required fields are marked *