Question

I am trying to get the order value from success.phtml page so that I can report it back in the facebook pixel.

I am just looking for the simplest way to get the order value. Yes, a shortcut pretty much.

I tried this:

$orderTotal = round($block->getGrandTotal(), 2);

and also

$lastOrderId = $block->getOrderId();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->load($lastOrderId);
echo "order id: ". $lastOrderId;
echo $order->getGrandTotal();

and several other methods but the total order value seems to be hard to find.

Was it helpful?

Solution

After doing even more tests I finally gave up and went oldschool and just simply created a new connection to the database and queried the sales_order table.

This is not the correct way to solve it. But I think there is a plugin or the template that is messing up my environment so in case exactly evertything else fails then I guess you can use this for a breif time.

$lastOrderId = $block->getOrderId();

$SQL = mysqli_connect('localhost', 'username', 'password', 'databasename', '8889');
$q = "select * from `sales_order` WHERE increment_id={$lastOrderId}";
$rs = $SQL->query($q);

$order_tmp = $rs->fetch_object();
$grand_total = $order_tmp->grand_total;  

OTHER TIPS

You can add this code to your success.phtml page to get the order total

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($block->getOrderId()); 
$total = $order->getGrandTotal(); ?>
<?php echo $total ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top