Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a link to Magento's My Account Page Conditionally

Tags:

magento

I would like to create a link on the My Account page that only get displays under certain conditions.

Right now I have the link display all the time by adding the following entry to my layout XML file:

<customer_account>
    <reference name="customer_account_navigation">
        <action method="addLink" translate="label" module="nie"><name>nie</name><path>nie</path><label>NIE Admin</label></action>
    </reference>
</customer_account>

I am assuming there is a way to code this so that it only displays under certain circumstances.

like image 495
Josh Pennington Avatar asked Jan 16 '11 15:01

Josh Pennington


1 Answers

The cart & checkout links already do something similar so their method can be copied.

  1. Create a block. It won't be displaying directly so can be descended from the boring Mage_Core_Block_Abstract.
  2. Give it a method where the conditional logic will go.

    public function addNieLink()
    {
        if (($parentBlock = $this->getParentBlock()) && (CONDITION-GOES-HERE)) {
            $parentBlock->addLink($this->_('NIE Admin'), 'nie', $this->_('NIE Admin'), true, array(), 50, null, 'class="top-link-cart"');
            // see Mage_Page_Block_Template_Links::addLink()
        }
    }
    
    protected function _prepareLayout()
    {
        // Add the special link automatically
        $this->addNieLink();
        return parent::_prepareLayout();
    }
    

    Put your check in place of CONDITION-GOES-HERE.

  3. Add your block to the links block.

    <customer_account>
        <reference name="customer_account_navigation">
            <block type="yourmodule/link" name="yourmodule.link" />
        </reference>
    </customer_account>
    

    (Correct the block type here to your newly created link block)

The important bit is it calls getParentBlock() to find out where the link is to go.

like image 57
clockworkgeek Avatar answered Nov 12 '22 05:11

clockworkgeek