How to check if customer is logged in or not in magento 2?

In Magento 2 we can check Customer is logged in or not by checking the given article,
Using the Block file, Pass Magento\Framework\App\Http\Context as a dependency to construct the method.

<?php

namespace Rbj\Customer\Block;

class Customerinfo extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\App\Http\Context $httpContext,
        array $data = []
    ) {
        $this->httpContext = $httpContext;
        parent::__construct($context, $data);
    }

    /*
     * return bool
     */
    public function getLogin() {
        return $this->httpContext->isLoggedIn();
    }
?>

Call getLogin function in the template file,

$isLoggedin = $block->getLogin();
if($isLoggedin) {
    // show your message
}

Never use Object Manager directly in real code in Magento 2.

Here I have shown just a demonstration using Objectmanager. Using Objectmanager directly create performance issue for the site.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$userContext = $objectManager->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $userContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
if($isLoggedIn) {
// do coding for customer loggin
}

How to get customer data by customer email in magento 2?

You can get customer information by just passing the customer email using below code snippet, Create Block file,

To Fetch Customer Data in Magento by customer email ID, you required a customer email id and an optional website id to fetch the correct data.

<?php
declare(strict_types=1);

namespace Rbj\Customer\Model;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

class CustomerDetail
{
    public function __construct(CustomerRepositoryInterface $customerRepository )
    {
        $this->customerRepository = $customerRepository;
    }

    /**
     * @param string $email
     * @param ?int $websiteId
     * @return CustomerInterface
     * @throws LocalizedException
     */
    public function getCustomerByEmail(string $email, int $websiteId = null): CustomerInterface
    {
        try {
            $customer = $this->customerRepository->get($email, $websiteId);
        } catch (NoSuchEntityException $exception) {
            throw new LocalizedException(__('Provided Customer no longer exists.'));
        }
        
        return $customer;
    }
}

Continue reading “How to get customer data by customer email in magento 2?”

How to get customer data by customer id in magento 2?

Magento 2 Retrieve Customer Data Object by ID with the help of interface CustomerRepositoryInterface.

You can get customer information by just passing the customer id using below code snippet,

I have written function inside Block class,

<?php

namespace Rbj\Customer\Block;

class Customer extends \Magento\Framework\View\Element\Template
{
    /**
     * Constructor
     *
     * @param \Magento\Framework\View\Element\Template\Context  $context
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
        array $data = []
    ) {
        $this->customerRepository = $customerRepository;
        parent::__construct($context, $data);
    }

    /* Pass customer id $id*/
    public function getCustomer($id)
    {   
        return $this->customerRepository->getById($id);
    }

Call getCustomer($id) function in template file,

$customerId = 1; //pass dynamic customer id
$getCustomer = $block->getCustomer($customerId);
echo $customer->getFirstname();  // result customer first name
echo $customer->getEmail(); // result as customer email 
echo $customer->getLastname(); // customerr lastname

Get all customer data by the below way,

echo “<pre>”;print_r($customer->__toArray());

Array
(
    [website_id] => 1
    [email] => roni_cost@example.com
    [group_id] => 1
    [store_id] => 1
    [created_at] => 2017-11-10 13:09:03
    [updated_at] => 2017-11-23 12:48:29
    [disable_auto_group_change] => 0
    [created_in] => Default Store View
    [prefix] => 
    [firstname] => Veronica
    [middlename] => 
    [lastname] => Costello
    [suffix] => 
    [dob] => 1973-12-15
    [default_billing] => 1
    [default_shipping] => 1
    [taxvat] => 
    [confirmation] => 
    [gender] => 2
    [addresses] => Array
        (
            [0] => Array
                (
                    [firstname] => Veronica
                    [lastname] => Costello
                    [street] => Array
                        (
                            [0] => 6146 Honey Bluff Parkway
                        )

                    [city] => Calder
                    [country_id] => US
                    [region] => Array
                        (
                            [region] => Michigan
                            [region_code] => MI
                            [region_id] => 33
                        )

                    [region_id] => 33
                    [postcode] => 49628-7978
                    [telephone] => (555) 229-3326
                    [id] => 1
                    [customer_id] => 1
                    [default_billing] => 1
                    [default_shipping] => 1
                )

        )

    [id] => 1
    [extension_attributes] => Array
        (
            [is_subscribed] => 
        )

)