0

I've tried various methods to get the customerID but an empty value is stored in the variable. Please advise :)

Below remnants of things I've tried. The file is located in the root.

require_once '/home/ab71714/public_html/app/Mage.php';
//Mage::app("default");

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

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

 if(Mage::getSingleton('customer/session')->isLoggedIn()) {
    $customerId = Mage::getModel('customer/session')->getCustomer()->getId();
} else {
    echo 'Not logged In';
}
CaitlinHavener
  • 1,408
  • 3
  • 24
  • 53
  • also check my answer here http://stackoverflow.com/questions/14743362/how-to-convert-standalone-php-files-to-magentos-mvc/14780910#14780910 as I ramble on about getting customer info (which isn't exactly easy) – Krista K Feb 15 '13 at 20:32
  • I've implemented these and get Call to a member function getId() on a non-object! – CaitlinHavener Feb 15 '13 at 21:26
  • If you see my `foreach( $dealers as $id => $obj)` the id is a number and `$obj` is the customer object. Test for the result from your `$customers=Mage::getModel('customer/customer')->getCollection()` to ensure that `$customers` has something. I use `print_r($customers)` for that. – Krista K Feb 15 '13 at 21:30

2 Answers2

2
require "/home/ab71714/public_html/app/Mage.php";
umask(0);
Mage::app();

$session = Mage::getSingleton('customer/session');
if($session->isLoggedIn()) {
    print_r($session);
    $customerId = $session->getCustomer()->getId();
} else {
    echo 'Not logged In';
}

For more information:

http://ka.lpe.sh/2011/06/19/magento-get-customer-details-customer-id-name-email/

Kalpesh
  • 5,635
  • 2
  • 23
  • 39
  • Kalpesh I've tried this and I get "Call to a member function getId() on a non-object". I realized that it wasn't getting past the if statement so I took the $customer= out of it and then the customer variable was a blank value. Any other ideas? – CaitlinHavener Feb 15 '13 at 21:27
  • @user1964129 i have edited the answer. can you please check it now? – Kalpesh Feb 16 '13 at 06:16
  • Yeah Im getting not logged in but I am! Is file placement an issue? – CaitlinHavener Feb 16 '13 at 23:17
  • do you want to get admin id (backend) or customer id (frontend)? – Kalpesh Feb 18 '13 at 08:20
2
if(Mage::getSingleton('customer/session')->isLoggedIn()) {
    $customerData = Mage::getSingleton('customer/session')->getCustomer();
    echo $customerData->getId();
}
julienc
  • 19,087
  • 17
  • 82
  • 82