i tried to save total order at customer session for not logged in and logged in customer in sales_order_save_after observer like this:

namespace Test\Custom\Observer;

use \Psr\Log\LoggerInterface;
use Magento\Framework\Event\ObserverInterface;
class SalesOrderAfter implements ObserverInterface
    protected $_checkoutSession;
    protected $_customerRepositoryInterface;
    protected $_addressInterface;

  public function __construct(
  \Magento\GoogleAdwords\Helper\Data $helper,
  \Magento\Sales\Model\ResourceModel\Order\Collection $collection,
    LoggerInterface $loggerInterface,
    \Magento\Framework\App\RequestInterface $request,
    \Magento\Framework\Registry $registry,
    \Magento\Sales\Model\OrderFactory $orderFactory,
  \Magento\Customer\Model\Session $session
      $this->_helper = $helper;
      $this->_collection = $collection;
      $this->_logger = $loggerInterface;
            $this->orderFactory = $orderFactory;
            $this->_request = $request;
            $this->registry = $registry;
      $this->session = $session;

    public function execute(\Magento\Framework\Event\Observer $observer)
    $order = $observer->getEvent()->getOrder();
    $order_id = $order->getIncrementId();

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $order = $objectManager->create('Magento\Sales\Model\Order')->load($order_id);
    $this->session->getCustomersSession()->setData('total_last_order', $order->getGrandTotal()); 
    return $this;

but i always got an error like this:

Fatal Error: 'Uncaught Error: Call to a member function setData() on null

1 Answer 1


You should use
$this->session->setData('total_last_order', $order->getGrandTotal());
when you use

\Magento\Customer\Model\Session $session

returns the customer session and you dont have to call getCustomerSession() separately

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.