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();