【问题标题】:Accessing custom variables from a .phtml in Magento在 Magento 中从 .phtml 访问自定义变量
【发布时间】:2011-08-08 19:26:49
【问题描述】:

我有一个文件 (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//客户/

编辑: 我需要访问用户的国家和用户 ID。 country 为 null 也没关系。

编辑: 我一直在做额外的研究,看起来我可以做这样的事情: Current user in Magento?

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

编辑:想通了:

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

【问题讨论】:

  • 您需要使用哪些对象/方法?您能否再描述一下您的 js.phtml 文件要完成的工作?
  • 您的路径有问题。默认主题模板文件夹的路径是\app\design\frontend\base\default\template

标签: php magento


【解决方案1】:

获取客户 ID:

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

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

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

【讨论】:

  • 有趣的是,getDefaultBillingAddress(),而不是我所做的。太棒了,我必须深入研究可能的差异。
【解决方案2】:

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

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

获取客户对象。那么……

echo $customer->getAddresses();

这应该会在地址中的某个位置显示国家/地区。至于用户名...我在文档页面上看不到它..您可以尝试

echo $customer->getAttributes();

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

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

【讨论】:

  • 很好的编辑。这篇文章也应该有帮助magentocommerce.com/boards/viewthread/22382
  • 抱歉,不是要编辑您的帖子。这是很好的信息。 +1。我把另一个作为答案,尽管他们打败了你。
  • 没问题。另一个帖子效率更高,我倾向于在我的帖子中闲逛一下。感谢 +1!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多