Skip to content

Instantly share code, notes, and snippets.

@veritstudio
Last active November 19, 2019 20:04
Show Gist options
  • Save veritstudio/478c738cd3053395c11d169903b04d4c to your computer and use it in GitHub Desktop.
Save veritstudio/478c738cd3053395c11d169903b04d4c to your computer and use it in GitHub Desktop.
Get customer details - Magento 2
<?php
namespace VeritStudio\CustomModule\Block;
use Magento\Customer\Model\Session;
class CustomBlock extends \Magento\Framework\View\Element\Template {
public function __construct(
Session $customerSession,
\Magento\Framework\View\Element\Template\Context $context
)
{
parent::__construct($context);
$this->_customerSession = $customerSession;
}
public function getCustomerId(){
return $this->_customerSession->getCustomer()->getId();
}
public function getCustomerName(){
return $this->_customerSession->getCustomer()->getName();
}
}
<block class="VeritStudio\CustomModule\Block\CustomBlock" name="customer.details" template="VeritStudio_CustomModule::details.phtml" cacheable="false" />
<div>
<p><?= __('Hello %s', $block->getCustomerName()) ?></p>
<p><?= __('Your customer id is %s, block->getCustomerId()) ?></p>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment