2011-08-08 31 views
0

我有一個執行代碼的文件(js.phtml)。我需要訪問一些用戶數據。當我在js.phtml中使用error_log($ this)時,它顯示「Mage_Core_Block_Template」 - 這讓我感到困惑。我期望這是父類,而不是傳遞給我的.phtml文件的類的價值。從Magento中的.phtml訪問自定義變量

那麼,我該如何與供應自定義對象或方法的一個.phtml文件?我是Magento的超級新手,我剛剛被放入任務中,因此我很抱歉使用錯誤的術語。

編輯: 我完整的文件路徑是:

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

還有一個完整的模塊DIR這裏:

  • /應用/代碼/本地//客戶/

編輯: 我需要訪問用戶國家和用戶ID。如果國家爲空,那也沒關係。

編輯: 我一直在做更多的研究,它看起來像我可以做這樣的事情: 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)); 
+0

您需要使用哪些對象/方法?你能描述一下你的js.phtml文件試圖完成什麼嗎? –

+0

在OP中更新,好點。 –

+0

你的路徑有問題。默認主題模板文件夾的路徑是'\ app \ design \ frontend \ base \ default \ template'。 – Zyava

回答

2

要獲得客戶ID:

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

要獲得國家 - 從哪個地址取決於。以下是從默認帳單地址獲取國家/地區代碼的示例:

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

有趣的getDefaultBillingAddress(),而不是我所做的。很酷,我不得不挖掘可能的差異。 –

1

我對Magento有點新,但我認爲這應該工作。

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

獲取客戶對象。然後...

echo $customer->getAddresses(); 

這應該得到國家在地址的某個地方。至於userid ...我沒有在文檔頁面上看到它..您可以嘗試

echo $customer->getAttributes(); 

然後看看裏面有什麼。文檔頁面是:

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

+1

很好的編輯。這篇文章也應該是有用的http://www.magentocommerce.com/boards/viewthread/22382/ –

+0

對不起,並不意味着編輯你的文章。儘管這是很好的信息。 +1。我把另一個作爲答案,但他們打敗你。 –

+0

不是問題。另一個帖子效率更高,我傾向於在帖子中漫不經心。欣賞+1! –

相關問題