简体   繁体   中英

Accessing custom variables from a .phtml in Magento

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));

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM