1

I want to inject Model into core block module. That it's a simple thing to do in the wrong way (Editing core block). But i want to know how to do it in the right way. Because i want to retrieve User data in the main page.

So, how can i do it, without editing core files?

Im customizing my templates and i want to show users name into the main page, Magento_CatalogWidget/templates/product/widget/content/grid.phtml.

To do that i need to inyect \Magento\Customer\Model\Session, the answer is where?

For now i have into grid.phtml the follwing code :

  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $name = $objectManager->create('\Magento\Customer\Model\Session')
          ->getCustomer()->getName();

Thanks!

0

3 Answers 3

1

You can inject customer model file into your block using __consturct() function.

public function __construct(
            \Magento\Customer\Model\Session $customerSession
        ) {
            $this->customerSession = $customerSession;
        }

    public function myCustom(){
        if ($this->customerSession->isLoggedIn()) {
            echo $this->customerSession->getCustomer()->getName();
        }
    }
12
  • Perfect! , i think my problem is how to put my block available to use in index page. And not in my own route.
    – Franco
    Dec 8, 2016 at 12:49
  • please check i have updated answer. Dec 8, 2016 at 13:06
  • Now i want to pass block to my own custom template. I have into cms_index_index.xml <block class="ObjectOut\Objeto\Block\Main" name="object_test" template=" " /> So how can i access to grid.phtml, that is in Magento_CatalogWidget inside my custom theme. To pass it into <block ... template="" >
    – Franco
    Dec 8, 2016 at 13:16
  • yes you can call function directly inside your template file Dec 8, 2016 at 13:16
  • please let me know if you have query Dec 8, 2016 at 13:20
1

In Magento 1 you'd get someone's session using:

Mage::getSingleton('core/session')

From there if you want to set a variable you can use:

$session = Mage::getSingleton('core/session')
$session->setVariableName('yourvalue')

And to retrieve your variable

$session->getVariableName();
1
  • Thanks! But i am working with Magento 2.
    – Franco
    Dec 8, 2016 at 12:24
1

core/session is not present in Magento2. There are these ones:

\Magento\Backend\Model\Session
\Magento\Catalog\Model\Session
\Magento\Checkout\Model\Session
\Magento\Customer\Model\Session
\Magento\Newsletter\Model\Session

You need to inject an instance of the session you need in your block. Let's take for example \Magento\Catalog\Model\Session.

protected $catalogSession;
public function __construct(
    ....
    \Magento\Catalog\Model\Session $catalogSession,
    ....
){
    ....
    $this->catalogSession = $catalogSession;
    ....
}

Then you can use the catalog session inside the class like this:

$this->catalogSession->setMyValue('test_value');
$this->catalogSession->getMyValue();

Hope it helps

1
  • 1
    I get that, but in which file i should inyect \Magento\Customer\Model\Session ? Thanks
    – Franco
    Dec 8, 2016 at 12:42

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.