5

Please anyone tell me, how to call checkout session in module's controller?

this method doesn't work for me

<?php

namespace Demo\Demo\Controller\Demo;


class Demo extends \Magento\Framework\App\Action\Action
{

    protected $_checkoutSession;

    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession
    ) {

        $this->_checkoutSession = $checkoutSession;
    }


    public function execute()
    {
        var_dump($this->_checkoutSession);
    }
}
1
  • Please share how it isn't working. What error(s) are you getting? Commented Dec 19, 2020 at 2:33

4 Answers 4

8

You can just try using below method,

<?php

namespace Demo\Demo\Controller\Demo;


class Demo extends \Magento\Framework\App\Action\Action
{

    protected $_checkoutSession;

    public function __construct(
        \Magento\Checkout\Model\Session $checkoutSession
    ) {

        $this->_checkoutSession = $checkoutSession;
    }


    public function execute()
    {
        var_dump($this->_checkoutSession->getData());exit;
    }
}
2
  • thanks for response, your response doesn't work i got this error: There has been an error processing your request Exception printing is disabled by default for security reasons. Error log record number: 326878733088 Commented Jun 13, 2016 at 13:19
  • Have a look in the var/report folder and see the error message in the "326878733088" file. Commented Jun 18, 2016 at 13:08
2

In the constructor, you need to make sure you call the parent::__construct method with the $context.

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Checkout\Model\Session $checkoutSession
    ) {
        $this->_checkoutSession = $checkoutSession;
        parent::__construct($context);
    }

0

You can use like this:

<?php

namespace Demo\Demo\Controller\Demo;

class Demo extends \Magento\Framework\App\Action\Action
{

    /** @var \Magento\Framework\View\Result\PageFactory  */
    protected $resultPageFactory;

    /** @var \Magento\Framework\Message\ManagerInterface  */
    protected $messageManager;


    public function __construct(
        \Magento\Framework\App\Action\Context $context,        
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->messageManager = $context->getMessageManager();
    }


    public function execute()
    {
        $session = $this->_objectManager->get('Magento\Checkout\Model\Session');
    }
}

Hope this helps!

1
  • 1
    Direct use of object manager is not a good practice in magento 2
    – Paras Sood
    Commented Apr 1, 2017 at 22:13
0

public function execute() { var_dump($this->_checkoutSession->getQuoteId());exit; }

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.