1

I was trying my hand on Magento 2. I am able to get the checkout session data inside a controller by injecting \Magento\Checkout\Model\Session $checkoutSession, in controllers construct method.

But when I try to do the same in a Block class, Im unable to get any data from check out session.

<?php
namespace Company\Hello\Block;

class Main extends \Magento\Framework\View\Element\Template
{
    protected $_checkoutSession;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Company\Hello\Model\Test $model,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Checkout\Model\Session $checkoutSession
    )
    {
        $this->_model   =   $model;
        $this->_request =   $request;
        $this->_checkoutSession = $checkoutSession;
        parent::__construct($context);
    }
    protected function _prepareLayout()
    {
        // print_r($this->_request->getParams());
        // exit;
        // $this->_model->getDbData();
        $this->setCntnt(__('Hello World 2.0'));
    }
    public function getAddedProduct()
    {
        foreach ($this->_checkoutSession->getQuote()->getAllVisibleItems() as $item) {
            echo $item->getName();
        }
        exit('block class');
    }
}

Whereas I am able to access the object ($this->_checkoutSession) but can't get any value.

I call the method getAddedProduct() in the template file

can someone clarify this please?

| improve this question | | | | |
1

You need to inject Checkout session and need to create()

public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
       \Magento\Checkout\Model\SessionFactory $session
    ) {
        $this->session= $session;
       parent::__construct($context);
    }

then use getAllVisibleItems

 $checkout = $this->session->create()->getQuote();
    foreach ($checkout->getAllVisibleItems() as $item) {  
      echo $item->getName();
    }

if work then plz accept the answere.

| improve this answer | | | | |
  • People! please add a comment why you downvoted this answer. This solution is working for me, but need to know why this was not recommended. Expert advise welcome. – Shathish Feb 9 '17 at 13:06
  • This is working for me as well +1 – B Karthik Kumar Jul 15 '17 at 12:01
0

In your construct method, add $this->_isScopePrivate = true;. Take a look: vendor/magento/module-checkout/Block/Onepage/Link.php

You can try cacheable="false" in your layout xml also.

Read more: How do disable caching of custom block on product view page?

| improve this answer | | | | |
  • setting _isScopePrivate didn't help, can you pls explain why "$this->_isScopePrivate = true;" should be set? – Shathish Feb 9 '17 at 13:08
  • @Shathish you should read more here: magento.stackexchange.com/a/93473/33057 – Khoa TruongDinh Feb 9 '17 at 13:24
  • @Shathish you also try with cacheable="false" in your layout xml. You should try again and let me know. – Khoa TruongDinh Feb 9 '17 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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