0

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

9
  • Please check your system.log or exception.log and tell me any error? Commented Dec 7, 2018 at 10:58
  • @RohanHapani nothing in there (relating to the page anyways) :s
    – treyBake
    Commented Dec 7, 2018 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. Commented Dec 7, 2018 at 11:12
  • @RohanHapani ok... how does one do this? :)
    – treyBake
    Commented Dec 7, 2018 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 Commented Dec 7, 2018 at 11:16

1 Answer 1

1

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.

2
  • it does work! You had a typo in your variable name for getCustomerID() and I just copied + pasted instead of reading it
    – treyBake
    Commented Dec 7, 2018 at 16:06
  • it's working fine using "Factory Method". Commented Jun 23, 2020 at 10:12

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.