13

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?

  • 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 Jun 22 '17 at 9:10
  • Check this link codextblog.com/code-snippet/… – Saravanan DS Sep 5 '18 at 13:10
24

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();
  • hi Raphel, i have take just reference from your answer. – Rakesh Jesadiya May 25 '16 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. – Raphael at Digital Pianism May 25 '16 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 – Raphael at Digital Pianism May 25 '16 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.. – Claudiu Creanga Jul 12 '16 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 – Raphael at Digital Pianism Jul 12 '16 at 14:29
4

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

2

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

  • 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). – Raphael at Digital Pianism May 25 '16 at 11:22

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.