Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento 2 - get order preview on success page

Tags:

html

php

magento2

Is there a way to show order information on success page after placing an order? The success phtml currently displays only order number information. The structure:

?>
<?php /** @var $block \Magento\Checkout\Block\Onepage\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;?>
            <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>

I tried with calling the getOrder() ?> function from sales module: Module_Sales/view/frontend/templates/order/view.phtml but it doesn't work.

like image 853
g5wx Avatar asked Jan 21 '16 10:01

g5wx


1 Answers

I'm about to do exactly the same thing, so I'll just document every step.

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

$session->clearQuote(); 

to

// $session->clearQuote();

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

like image 60
Timon de Groot Avatar answered Sep 17 '22 21:09

Timon de Groot