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