I'm building a module where it needs the Customer ID to filter some data in a table.

I currently have my Block class set up like this:

namespace TreyBake\Garage\Block;

use Magento\Framework\View\Element\Template;

class Garage extends Template
{
    protected $garageCollectionFactory;
    protected $customerSession;

    public function __construct(
        Template\Context $context,
        \TreyBake\Garage\Model\ResourceModel\Garage\CollectionFactory $garageCollectionFactory,
        \Magento\Customer\Model\Session $customerSession
    )
    {
        $this->garageCollectionFactory = $garageCollectionFactory;
        $this->customerSession = $customerSesssion;

        parent::__construct($context);
    }

    public function getGarageCollection()
    {
        return $this->garageCollectionFactory->create();
    }

    public function getCustomerID()
    {
        return $this->customerSession->getCustomer()->getId();
    }
}

and I try to echo it out in my template file like this:

ini_set('display_errors', 1); ini_set('display_startup_errors', 1);
error_reporting(-1);

$collection = $this->getGarageCollection();
$collectionCount = count($collection);

echo $this->getCustomerID();

but this outputs nothing: enter image description here

weirder yet, if I remove that echo $this->getCustomerID(); my other content loads e.g. there's an if statement based on $collectionCount - this outputs the correct string. When the echo is there it doesn't output anything (as displayed in the img above).

What am I doing wrong?

if you need more code/files - lemme know.

Edit:

I've adjusted my Block code to this:

public function getCustomerID()
{
    $customer = $this->customerSession->create();
    return $customer->getCustomer()->getId();
}

still nothing displayed :S

  • Please check your system.log or exception.log and tell me any error? – Rohan Hapani Dec 7 at 10:58
  • @RohanHapani nothing in there (relating to the page anyways) :s – treyBake Dec 7 at 11:09
  • Instead of print echo in file. check customer id by log file. Try to check it. Still, if you'll get blank then inform here. – Rohan Hapani Dec 7 at 11:12
  • @RohanHapani ok... how does one do this? :) – treyBake Dec 7 at 11:13
  • Remove echo and add this code => $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log'); $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); $logger->info($this->getCustomerID()); Go to var/log folder and check test.log file – Rohan Hapani Dec 7 at 11:16
up vote 0 down vote accepted

Try to use Factory Method :

protected $_customerSessionFactory;

public function __construct(
    Template\Context $context,
    \TreyBake\Garage\Model\ResourceModel\Garage\CollectionFactory $garageCollectionFactory,
    \Magento\Customer\Model\SessionFactory $customerSessionFactory
)
{
    $this->garageCollectionFactory = $garageCollectionFactory;
    $this->_customerSessionFactory = $customerSessionFactory;

    parent::__construct($context);
}

public function getCustomerID()
{
    return $this->_customerSessionFactory->create();
}

and add use code in template file :

echo $block->getCustomerID()->getCustomer()->getId();

Remove genereated and flush cache.

  • it does work! You had a typo in your variable name for getCustomerID() and I just copied + pasted instead of reading it – treyBake Dec 7 at 16:06

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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