16

I'm trying to get the order total on the success page on magento 2 , I have added this code

<?php
$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$totall = $order->getGrandTotal();
?>

But I'm getting an error when I reach the success page after I place an order, I guess the code works for magento 1 but not 2. How can I get this working also on magento 2?

2
  • Hi @alexcr you use magento 1 command to get data. Just put below code to get order data in success page in magneto 2. $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $orderData = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($block->getOrderId()); echo "<pre>";print_r($orderData->getData());
    – Nikul
    Commented Jun 22, 2017 at 9:10
  • Check this link codextblog.com/code-snippet/… Commented Sep 5, 2018 at 13:10

5 Answers 5

32

Magento 2.1

The block mentioned below is now Magento\Checkout\Block\Onepage\Success

Magento 2.0

The only thing you can retrieve natively on this page is the order id using the getRealOrderId() method defined in Magento\Checkout\Block\Success

Thus, to get the order id you can call the following in your template:

$block->getRealOrderId();

However, I understand that's not exactly what you need.

In that case, even though you could use the object manager directly, it is not recommended. You should use a custom module to define prefences for this block.

In app/code/Vendor/Module/etc/frontend/di.xml you need the following code:

<?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\Success"
                type="Vendor\Module\Block\Checkout\Success"/>
</config>

Then in app/code/Vendor/Module/Block/Checkout/Success.php :

<?php
namespace Vendor\Module\Block\Checkout;

class Success extends \Magento\Checkout\Block\Success
{
    /**
     * @return int
     */
    public function getGrandTotal()
    {
        /** @var \Magento\Sales\Model\Order $order */
        $order = $this->_orderFactory->create()->load($this->getLastOrderId());
        return $order->getGrandTotal();
    }
}

Don't forget the usual app/code/Vendor/Module/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_Module" setup_version="0.0.1" />
</config>

As well as the app/code/Vendor/Module/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

Once you're done and you've ran the following commands:

php bin/magento module:enable Vendor_Module
php bin/magento setup:upgrade

You should be able to call the following in your template:

$block->getGrandTotal();

Adding more methods

You can add the following that can be useful when tracking to the block class:

public function getSubtotal()
{
    /** @var \Magento\Sales\Model\Order $order */
    $order = $this->_orderFactory->create()->load($this->getLastOrderId());
    return $order->getSubtotal();
}

public function getDiscountAmount()
{
    /** @var \Magento\Sales\Model\Order $order */
    $order = $this->_orderFactory->create()->load($this->getLastOrderId());
    return $order->getDiscountAmount();
}

Then you'll be able to call the following from your template:

$block->getSubtotal();
$block->getDiscountAmount();
10
  • hi Raphel, i have take just reference from your answer. Commented May 25, 2016 at 13:15
  • 2
    @Rakesh I have no problem with you referencing my answer, we are here to learn and teach, it is just not fair to simply copy/paste the upvoted answer because you are downvoted. The original poster accepted your original answer, which means your answer is correct and worked for him. However, as I said in my answer and as David Manners said in the comment, it is not recommended and should be avoided whenever it's possible. Commented May 25, 2016 at 13:19
  • @Rakesh well I'm not responsible for upvotes and downvotes. And copy/pasting other's answers will not help you getting upvoted. Again your answer is correct for every users, it will work everywhere but it is discouraged to use the ObjectManager directly. We're here to learn and I'm sure you will try to avoid using the ObjectManager directly in your future answers Commented May 25, 2016 at 13:23
  • 1
    Hi Raphael, if your example doesn't work and app/code/Vendor/Module/Block/Checkout/Success.php is not rendered at all (even if I insert die() or illegal php syntax), does it mean that it is overwritten somewhere else? I run the compile.. Commented Jul 12, 2016 at 14:20
  • @ClaudiuCreanga it could be. Last time I tested that was on early Magento 2.0 I haven't tried the code on latest 2.1 releases. You can use echo get_class($block); to check if the override works but if the block isn't rendered at all that's probably not gonna work Commented Jul 12, 2016 at 14:29
7

Just open Magento_Checkout/frontend/templates/success.phtml

And put below code in File

    $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
              $orderData = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($block->getOrderId());
echo "<pre>";print_r($orderData->getData());

In above code you will get all order data in success page.

Thanks

1
3

As far as I know, there is an event - checkout_onepage_controller_success_action which is fired after Checkout one page is successfully.

vendor/magento/module-checkout/Controller/Onepage/Success.php

public function execute()
{
    $session = $this->getOnepage()->getCheckout();
    if (!$this->_objectManager->get('Magento\Checkout\Model\Session\SuccessValidator')->isValid()) {
        return $this->resultRedirectFactory->create()->setPath('checkout/cart');
    }
    $session->clearQuote();
    //@todo: Refactor it to match CQRS
    $resultPage = $this->resultPageFactory->create();
    $this->_eventManager->dispatch(
        'checkout_onepage_controller_success_action',
        ['order_ids' => [$session->getLastOrderId()]]
    );
    return $resultPage;
}

As we can see, we can get the order id by using Observer. For example:

public function execute(\Magento\Framework\Event\Observer $observer)
{

    $orderIds = $observer->getEvent()->getOrderIds();
    if (empty($orderIds) || !is_array($orderIds)) {
        return $this;
    }

    //.......

    $block = $this->_layout->getBlock('your_block_here');
    if ($block) {
        $block->setOrderIds($orderIds);
    }
}

Take a look at the google modules for more details:
vendor/magento/module-google-adwords
vendor/magento/module-google-analytics

1
  • I like the idea of using an observer, however the way you're doing it is kind of useless because there's already a method to retrieve the order id in the block (see my answer). Commented May 25, 2016 at 11:22
2

I tried Raphaels solution but it did not work on my system (Magento 2.3.6).

I modified the Success.php like this and this solution worked for me:

<?php
namespace MODULE\NAME\Block\Checkout;

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

    public function getId()
    {
        $lastorderId = $this->_checkoutSession->getLastRealOrder()->getIncrementId();
        return $lastorderId;
    }
    
    public function getGrandTotal()
    {
        $order = $this->_checkoutSession->getLastRealOrder();
        return $order->getGrandTotal();
    }

    public function getSubtotal()
   /** Total net value of order without taxes */
    {
    $order = $this->_checkoutSession->getLastRealOrder();
    return $order->getSubtotal();
    }

    public function getTaxAmount()
    {
    $order = $this->_checkoutSession->getLastRealOrder();
    return $order->getTaxAmount();
    }
    
}
?>
0

Magneto 2.1.x, 2.3.x and 2.4.x

Create custom module and Override Magento\Checkout\Block\Onepage\Success

#1 Create: app/code/VendorName/ModuleName/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
); 

#2 create: app/code/VendorName/ModuleName/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="VendorName\ModuleName\Block\Onepage\Success"/>
</config>

#2 create: app/code/VendorName/ModuleName/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_Module" setup_version="0.0.1" />
</config>

#4 create: app/code/VendorName/ModuleName/Block/Onepage/Success.php

<?php
namespace VendorName\ModuleName\Block\Onepage;

class Success extends \Magento\Checkout\Block\Onepage\Success
{
    /**
     * @return int
     */
    public function getGrandTotal()
    {
        /** @var \Magento\Sales\Model\Order $order */
       $order = $this->_checkoutSession->getLastRealOrder();
        return $order->getGrandTotal();
    }
}

#5 on success page template Magento_Checkout/templates/success.phtml use

$block->getGrandTotal();

if mode production:

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
php bin/magento indexer:reindex
php bin/magento cache:flush

else

php bin/magento setup:upgrade
php bin/magento cache:flush

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.