2012-11-15 47 views
0

我創建了一個Magento擴展程序,並且希望以編程方式將鏈接添加到「我的帳戶」導航中。我已閱讀以下線索(Magento - How to add/remove links on my account navigation?)及其參考的網站,但他們只討論靜態添加鏈接。在magento中動態添加「我的帳戶導航」鏈接

通過將以下內容添加到我的模塊中的佈局文件中,我可以獲得一個新的鏈接以顯示在客戶帳戶導航中。

<customer_account> 
    <reference name="customer_account_navigation"> 
     <action method="addLink" translate="label" module="mymodule"> 
      <name>modulename</name> 
      <path>mymodule/</path> 
      <label>New link</label> 
     </action> 
    </reference> 
</customer_account> 

我該如何做到這一點,以便該鏈接的外觀取決於調用我的一個擴展模型的方法的結果。

回答

0

你必須使用的Magento 你必須使用它「controller_action_layout_load_before」 在你的模塊的config.xml事件的事件觀察器功能

<controller_action_layout_load_before> 
    <observers> 
     <uniquename> 
      <type>singleton</type> 
      <class>Package_Modulename_Model_Observer</class> 
      <method>customlink</method> 
     </uniquename> 
    </observers> 
</controller_action_layout_load_before> 

並在相應observer.php使用以下代碼

public function customlink(Varien_Event_Observer $observer) 
{ 
    $update = $observer->getEvent()->getLayout()->getUpdate(); 
    $update->addHandle('customer_new_handle'); 
} 

和local.xml中寫

<customer_new_handle> 
    <reference name="customer_account_navigation"> 
     <action method="addLink" translate="label" module="mymodule"> 
      <name>modulename</name> 
      <path>mymodule/</path> 
      <label>New link</label> 
     </action> 
    </reference> 
</customer_new_handle> 
+0

感謝您的建議,我試過了,它確實有效。然而,它看起來效率很低,因爲它會調用我的方法(訪問數據庫)每次頁面加載,而不管訪問者是否登錄。這是否真的是實現此目的的最佳/唯一方法? – Dom

+0

好吧,我明白了你的觀點......如果你想避免執行整個功能,如果客戶沒有登錄,你可以檢查客戶的會話是否設置inisde .. 公共職能customlink(Varien_Event_Observer $觀察員){ } 我不能說這是最好的方法...如果你有任何分享它在這裏 – Leo

0

我想你應該能夠使用Magento的使用ifconfig屬性作爲解釋here

2

我已經遇到此相同的需求,這是實現這一目標的最佳途徑,我發現。

1)用下面的代碼創建一個擴展Magento帳戶導航塊的新Block文件。

class Mynamespace_Mymodule_Block_Addlink extends Mage_Customer_Block_Account_Navigation { 
    public function addLinkToUserNav() { 
     if (your logic here) { 
      $this->addLink(
        "your label", 
        "your url", 
        "your title" 
      ); 
     } 
    } 
} 

2)在擴展的配置文件config.xml中,添加以下代碼(尊重您存在XML數據結構):

<config> 
    ... 
    <global> 
     .... 
     <blocks> 
      ... 
      <customer> 
       <rewrite> 
        <account_navigation>Mynamespace_Mymodule_Block_Addlink</account_navigation> 
       </rewrite> 
      </customer> 
     </blocks> 
    </global> 
</config> 

3)在您的擴展XML佈局文件中,添加下面的代碼(尊重你現有的XML數據結構):

<layout> 
    ... 
    <customer_account> 
     <reference name="customer_account_navigation"> 
      <action method="addLinkToUserNav"></action> 
     </reference> 
    </customer_account> 
</layout> 

就是這樣。這將爲您提供動態添加/刪除帳戶導航鏈接的功能。

相關問題