Dom
Dom

Reputation: 3080

Dynamically add links to 'my account navigation' in magento

I'm creating a Magento Extension and want to be programatically add links to the 'My account' navigation. I've read the following thread (Magento - How to add/remove links on my account navigation?) and the sites it references, but they only talk about adding a link statically.

By adding the following to a layout file in my module I can get a new link to appear in the customer account navigation.

<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>

How can I make it so that this links appearance depends on the results of a method call to one of my extensions models.

Upvotes: 0

Views: 2234

Answers (3)

I've come across this same need and this is the best way to achieve it I've found.

1) Create a new Block file that extends the Magento Account Navigation Block, with the following code.

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) In your extension config file config.xml, add the following code (respecting your existent XML data structure):

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

3) In your extension XML layout file, add the following code (respecting your existent XML data structure):

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

And this is it. This will provide you the ability to dynamically add/remove account navigation links.

Upvotes: 2

Leo
Leo

Reputation: 439

You have to use event-observer functionality of magento The event you have to use it "controller_action_layout_load_before" In your module's 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>

and in the corresponding observer.php use the following code

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

and in local.xml write

<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>

Upvotes: 0

erwin_smit
erwin_smit

Reputation: 700

I think you should be able to use Magento's ifconfig attribute as explained here

Upvotes: 0

Related Questions