Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Magento customer ID

Tags:

php

magento

Ugh how do I get the customer ID!!? These are all things I've tried! Can you see what I'm doing wrong?

//include_once "app/Mage.php";
require_once '/home/ab71714/public_html/app/Mage.php';

//Mage::app("default");

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

if($customer = Mage::getSingleton('customer/session')->isLoggedIn()) {
    $customerData = Mage::getModel('customer/customer')->load($customer->getId())->getData();
    print_r($customerData);
    echo $customerData->getId();
}

//$customerData = Mage::getModel('customer/customer');
//$customerID = $customerData -> getId(); 

//$userinfo = $customerData->_origData; // fetch users info
$customerID=$customer -> getId(); 
//$customerID = $customerData->getEntityId();
//$customerID = $customerData[entity_id];
like image 628
CaitlinHavener Avatar asked Feb 15 '13 21:02

CaitlinHavener


People also ask

How can I get Magento customer ID?

The function isLoggedIn will only return a boolean as to if a customer is logged in and no other information. The customer session does have to following functions: getCustomerId : which will return the customer id. getCustomer : which will return the customer object.

How can I get current customer ID in magento2?

We will Get Customer ID Without Session in Magento 2. You can get the customer Id using through the customer session but if you want to get customer id without using customer session you can you the follow code. > A NULL value is returned even if the customer is logged in.

What is customer session in Magento 2?

Magento\Checkout\Model\Session– Checkout session is used to store checkout related information. Magento\Customer\Model\Session– Customer session is used for customer, frontend login and all other activities.


3 Answers

Try

 if(Mage::getSingleton('customer/session')->isLoggedIn()) {      $customerData = Mage::getSingleton('customer/session')->getCustomer();       echo $customerData->getId();  } 

See Current user in Magento?

like image 109
Renon Stewart Avatar answered Oct 20 '22 12:10

Renon Stewart


The fastest way is

Mage::getSingleton('customer/session')->getId() 
like image 20
Shadowbob Avatar answered Oct 20 '22 12:10

Shadowbob


The function isLoggedIn will only return a boolean as to if a customer is logged in and no other information.

The customer session does have to following functions:

  1. getCustomerId: which will return the customer id

  2. getCustomer: which will return the customer object.

like image 43
dmanners Avatar answered Oct 20 '22 10:10

dmanners