Magento 2 Get order total on success page?
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();
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
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