繁体   English   中英

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

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

我有一个执行我的代码的文件(js.phtml)。 我需要访问一些用户数据。 当我在js.phtml中的error_log($ this)时,它显示“ Mage_Core_Block_Template”-这让我感到困惑。 我希望它是父类,而不是传递给我的.phtml文件的类的值。

那么,如何为.phtml文件提供自定义对象或方法? 我对Magento来说是个超级新手,我刚刚开始从事一项工作,因此对可能使用不正确的术语表示歉意。

编辑:我的完整文件路径是:

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

这里还有一个完整的模块目录:

  • / app / code / local // Customer /

编辑:我需要访问用户国家和用户ID。 国家为空也可以。

编辑:我一直在做其他研究,看来我可以做这样的事情: Magento中的当前用户?

但是我还没有在代码中尝试过...。

编辑:弄清楚了:

error_log(print_r(Mage::getSingleton('customer/session')->getCustomer()->getEntityId(), true));
error_log(print_r(Mage::getSingleton('customer/session')->getCustomer()->getCountry(), true));

获取客户ID:

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

获取国家/地区-取决于来自哪个地址。 这是从默认帐单地址获取国家/地区代码的示例:

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

我对Magento有点陌生,但是我认为这应该可行。

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

那得到顾客对象。 然后...

echo $customer->getAddresses();

那应该使该国在地址中某处。 至于用户名...我在文档页面上没有看到它。

echo $customer->getAttributes();

看看里面有什么。 文档页面位于:

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