cancel
Showing results for 
Search instead for 
Did you mean: 

Get grand total by id using DI

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.

16 REPLIES 16

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

Problem solved? Click Accept as Solution!

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

Ankit Jasani

Re: Get grand total by id using DI

Hi @Bhanu Periwal 

I get the same error !

Maybe because of calling function in .phtml not correct ?

Re: Get grand total by id using DI

Hi @Ankit Jasani 

This tutorial didn't explain how to get data separately !

Could you please tell me how i can get specific data from the order ? 

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 )

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

Problem solved? Click Accept as Solution!

Re: Get grand total by id using DI

Hi @Bhanu Periwal 

Yes it's work, but as I read it's not recommended to use object manager.

Is it safe ?

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

Problem solved? Click Accept as Solution!

Re: Get grand total by id using DI

Hi @Bhanu Periwal 

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 ?