Magento – How to Create Dynamic Link in Customer Account Navigation

layout-updatenavigation

I want to create a link dynamically in customer_block_account_navigation (link supposed to show up in the sidebar)

A static link isn't a problem, you can achieve that with a xml layout update:

<customer_account>
    <reference name="customer_account_navigation">
        <action method="addLink" translate="label" module="customer">
            <name>mymodulename</name>
            <path>mypath</path>
            <label>Label of the link</label>
        </action>
    </reference>
</customer_account>

The link goes to a custom form which supposed to show up only for certain customer. So I need to control the link (or layout update) via PHP in my own module. But I couldn't find any solution how to do that.

Can anyone help me?

Thank you so much.

Best Answer

You can add a block inside the customer_account_navigation and in that block, add the link to the parent block if your conditions are satisfied.

So in your layout goes this:

<customer_account>
    <reference name="customer_account_navigation">
        <block type="[module]/customer_link" as="[module]_customer_link" name="[module]_customer_link">
            <action method="addLinkToParentBlock" />
        </block>
    </reference>
</customer_account>

And your block class shoud look like this:

class [Namespace]_[Module]_Block_Customer_Link extends Mage_Core_Block_Abstract 
{
    public function addLinkToParentBlock() 
    {
        $parent = $this->getParentBlock();
        if ($parent) {
            if (your condition goes here) {
                $parent->addLink(
                    'Label goes here',
                    'Url goes here',
                    'title goes here',
                );

            }
        } 
    }
}

This way, your block will get instantiated. It will have no output, but the method addLinkToParentBlock will be called.

Related Topic