:::: MENU ::::
Posts tagged with: customer

Magento 2 current customer group

Today we talk about how in Magento 2 current customer group. This tutorial includes example of get current customer group id, get current customer group name and other group information. Magento 2 use the model session for storing the current session data. You have to inject \Magento\Customer\Model\Session to get current customer session data and inject \Magento\Customer\Model\Group to get customer group data. So let start with our example.

Dependency Injection

protected $_customerSession;
protected $_customerGroupCollection;

public function __construct(
           
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Model\Group $customerGroupCollection,

    ) {


        $this->_customerSession = $customerSession;
        $this->_customerGroupCollection = $customerGroupCollection;

    }

public function getGroupId(){
 if($this->_customerSession->isLoggedIn()):
   //Get current group
        echo $customerGroupId = $this->_customerSession->getCustomer()->getGroupId();
                $groupCollection = $this->_customerGroupCollection->load($customerGroupId);
        echo $groupCollection>getCustomerGroupCode();//Get current customer group name
    endif;

}

Object Manager

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

if ($customerSession->isLoggedIn()) {

//Get current group
        echo $customerGroupId = $customerSession->getCustomer()->getGroupId();
                $groupCollection = $objectManager->create('\Magento\Customer\Model\Group')->load($customerGroupId);
        echo $groupCollection>getCustomerGroupCode();//Get current customer group name
   }
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018


Magento 2 check if customer logged in or not

Today we talk about how in Magento 2 check if customer logged in or not. This tutorial includes check if customer logged in or not.Magento 2 use the model session for storing the current customer session data like customer id, customer email, customer name and other customer information. You have to inject \Magento\Customer\Model\Session to get current customer session data. So let start with our example.

Dependency Injection

protected $_customerSession;



public function __construct(
           
        \Magento\Customer\Model\Session $customerSession,

    ) {


        $this->_customerSession = $customerSession;

    }

public function getGroupId(){
 if($this->_customerSession->isLoggedIn()):
   //Get current group
         echo $this->_customerSession->getCustomer()->getId();  // get customer id
         echo $this->_customerSession->getCustomer()->getName();  // get  customer Full Name
         echo $this->_customerSession->getCustomer()->getEmail(); // get customer Email
else:

   echo “no logged in”;            


endif;

}

Object Manager

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

if ($customerSession->isLoggedIn()):
   //Get current group
         echo $this->_customerSession->getCustomer()->getId();  // get customer id
         echo $customerSession->getCustomer()->getName();  // get  customer Full Name
         echo $customerSession->getCustomer()->getEmail(); // get customer Email
else:

   echo “no logged in”;            


endif;
Author: Qaisar Satti
Category: Magento 2
Last Modified: July 12, 2018