0

I have a file (js.phtml) where my code executes. I need to access some user data. When I error_log($this) in js.phtml, it displays, "Mage_Core_Block_Template" - which is confusing to me. I would expect that to be the parent class, not the value of the class getting passed to my .phtml file.

So, how do I supply the .phtml file with custom objects or methods? I'm super new to Magento and I've just been dropped into a task, so I apologize for possibly using incorrect terminology.

Edit: My full file path is:

  • /app/design/frontend//default/template/customer/welcome/js.phtml

There is also a full module dir here:

  • /app/code/local//Customer/

Edit: I need access to the users country and userid. It's ok if country is null.

Edit: I've been doing additional research, it looks like I can do something like this: Current user in Magento?

But I haven't tried it in code yet ....

Edit: Figured it out:

error_log(print_r(Mage::getSingleton('customer/session')->getCustomer()->getEntityId(), true));
error_log(print_r(Mage::getSingleton('customer/session')->getCustomer()->getCountry(), true));
Community
  • 1
  • 1
mr-sk
  • 13,174
  • 11
  • 66
  • 101

2 Answers2

2

To get customer ID:

echo Mage::getSingleton('customer/session')->getCustomer()->getId();

To get country - depends from which address. Here's an example of getting the country code from the default billing address:

echo Mage::getSingleton('customer/session')->getCustomer()->getDefaultBillingAddress()->getCountry();
djdy
  • 6,779
  • 6
  • 38
  • 62
  • Interesting, getDefaultBillingAddress(), instead of what I did. Cool, I'll have to dig into the possible difference. – mr-sk Aug 08 '11 at 20:06
1

I am somewhat new to Magento, but I think this should work.

$customer = Mage::getSingleton('customer/session')->getCustomer();

That gets the customer object. Then...

echo $customer->getAddresses();

That should get the country somewhere in the address. As for userid...I don't see it on the documentation page..You could try

echo $customer->getAttributes();

And see what is in there. The documentation page is at:

http://docs.magentocommerce.com/Mage_Customer/Mage_Customer_Model_Customer.html

mr-sk
  • 13,174
  • 11
  • 66
  • 101
Nathaniel Wendt
  • 1,194
  • 4
  • 23
  • 49