1

I want to show customer last name into my website when customer will access their account.

<?php if( $isLoggedIn ) { ?>                                    
<li class="first">
    <a id="quick_sigup_link" style="positon:relative;" href="<?php echo $this->getUrl('customer/account/'); ?>">
            <span>Hello. User</span>
            <span class="youraccount" style="margin-top:-25px;">My Account</span>
    </a>
</li>
1

Can be done using plugin and knockout

https://github.com/DominicWatts/CustomerLogin/blob/master/Plugin/Magento/Customer/CustomerData/Customer.php#L45

    /**
     * @param \Magento\Customer\CustomerData\Customer $subject
     * @param $result
     * @return mixed
     */
    public function afterGetSectionData(\Magento\Customer\CustomerData\Customer $subject, $result)
    {
        $result['is_logged_in'] = $this->customerSession->isLoggedIn();
        if ($this->customerSession->isLoggedIn() && $this->customerSession->getCustomerId()) {
            $customer = $this->customerSession->getCustomer();
            $result['email'] = $customer->getEmail();
            $result['lastname'] = $customer->getLastname();
            $result['customer_group_id'] = $customer->getGroupId();
            $result['customer_group_name'] = $this->getGroupName($customer->getGroupId());
        }

        return $result;
    }

https://github.com/DominicWatts/CustomerLogin/blob/master/view/frontend/templates/index/index.phtml#L3-L5

<div class="greet welcome" data-bind="scope: 'customer'">
    <!-- ko if: customer().is_logged_in  -->
    <span class="logged-in"
            data-bind="text: new String('<?= $block->escapeHtml(__('Welcome back %1', '%1')) ?>').replace('%1', customer().lastname)">
    </span>
    <!-- /ko -->   
</div>
0

First You need to create a object instance:

$obj = \Magento\Framework\App\ObjectManager::getInstance();

After That:

    <?php
        $customerSession = $obj->get('Magento\Customer\Model\Session');
        if($customerSession->isLoggedIn()) {
 echo   $customerSession->getCustomer()->getLastname()."<br/>";  // get Customer last name
            echo   $customerSession->getCustomer()->getName()."<br/>";  // get  Full Name

          }  
  • Direct Objectmanager instance is a quick fix but should be avoided as much as possible. good idea would be to inject in the constructor. – mp196 Jan 9 '17 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.