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

Magento 2 add link customer navigation

Today we discuss about Magento 2 add link customer navigation.This tutorial include how you can add your custom link in customer account navigation, Magento 2 add link customer navigation. So let’s start with our example.

If you are adding it from theme layout then create default.xml in following path.

app/design/frontend/QaisarSatti/theme/Magento_Theme/layout/default.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>  

    <referenceBlock name="customer_account_navigation">
                <block class="Magento\Customer\Block\Account\SortLinkInterface" name="test-link">
                    <arguments>
                        <argument name="path" xsi:type="string">test/controller/action</argument>
                        <argument name="label" xsi:type="string">Test Link</argument>
                         <argument name="sortOrder" xsi:type="number">150</argument>
                    </arguments>
                </block>
    </referenceBlock>

  </body>
</page>

Or you want it to add from module layout then create default.xml in following path.

app/code/QaisarSatti/view/HelloWorld/frontend/layout/default.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>  

    <referenceBlock name="customer_account_navigation">
                <block class="Magento\Customer\Block\Account\SortLinkInterface" name="test-link">
                    <arguments>
                        <argument name="path" xsi:type="string">test/controller/action</argument>
                        <argument name="label" xsi:type="string">Test Link</argument>
                         <argument name="sortOrder" xsi:type="number">150</argument>
                    </arguments>
                </block>
    </referenceBlock>

  </body>
</page>
Author: Qaisar Satti
Category: Magento 2
Last Modified: August 6, 2018


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