- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Get grand total by id using DI
After a long suffering, I found that I've to use DI instead of object manager. However I'm trying to create block in custom module and call the method in success.phtml. But I got the following error : An error has happened during application run. See exception log for details.
My block file :
<?php namespace Nectar\Google\Block; private $_objectManager; public function __construct(\Magento\Framework\ObjectManagerInterface $objectmanager) { $this->_objectManager = $objectmanager; } public function GrandTotal() { $oid = $this->_objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($block->getOrderId()); return $order->getGrandTotal($oid); }
In success.phtml :
$block = $this->block(Nectar\Google\Block\order); $total = $block->GrandTotal();
I feel that I mad a mistake but I don't know where.. could you please help me.
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get grand total by id using DI
Hello @infonectar9054
Kindly refer below code:
<?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(); } }
It may help you!
Thank you
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get grand total by id using DI
Hi @infonectar9054 ,
Please refer this one:
https://github.com/jothibasuj/M2--Order-success-page-customization
Problem Solved? Accept as solution.
Hope it helps!
Thanks
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get grand total by id using DI
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get grand total by id using DI
This tutorial didn't explain how to get data separately !
Could you please tell me how i can get specific data from the order ?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get grand total by id using DI
Hi Friends,
Please note that I want to call the function in success.phtml in ( Magento_InventoryInStorePickupFrontend )
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get grand total by id using DI
Hello @infonectar9054
Try this code with phtml file directly.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $orders = $objectManager->create('Magento\Sales\Model\Order')->load($block->getOrderId()); echo $grandTotal= $orders->getGrandTotal();;
It may help you!
Thank you
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get grand total by id using DI
Yes it's work, but as I read it's not recommended to use object manager.
Is it safe ?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get grand total by id using DI
Hello @infonectar9054
If it works for you then use this code under layout block file and use block function instead of this code.
Take help of developer in this case.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Re: Get grand total by id using DI
I'll try to do it by my self.
I'll create custom module and put this code in function inside a class in block file then call the function in phtml.
Is that correct ?