简体   繁体   English

从Magento中的.phtml访问自定义变量

[英]Accessing custom variables from a .phtml in Magento

I have a file (js.phtml) where my code executes. 我有一个执行我的代码的文件(js.phtml)。 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. 当我在js.phtml中的error_log($ this)时,它显示“ Mage_Core_Block_Template”-这让我感到困惑。 I would expect that to be the parent class, not the value of the class getting passed to my .phtml file. 我希望它是父类,而不是传递给我的.phtml文件的类的值。

So, how do I supply the .phtml file with custom objects or methods? 那么,如何为.phtml文件提供自定义对象或方法? I'm super new to Magento and I've just been dropped into a task, so I apologize for possibly using incorrect terminology. 我对Magento来说是个超级新手,我刚刚开始从事一项工作,因此对可能使用不正确的术语表示歉意。

Edit: My full file path is: 编辑:我的完整文件路径是:

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

There is also a full module dir here: 这里还有一个完整的模块目录:

  • /app/code/local//Customer/ / app / code / local // Customer /

Edit: I need access to the users country and userid. 编辑:我需要访问用户国家和用户ID。 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? 编辑:我一直在做其他研究,看来我可以做这样的事情: 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: 获取客户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. 我对Magento有点陌生,但是我认为这应该可行。

$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 http://docs.magentocommerce.com/Mage_Customer/Mage_Customer_Model_Customer.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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