Magento 1.9 – How to Hide Top Links from Customer Groups

magento-1.9toplinksxml

Is it possible to hide certain top links depending on the customer group?
As we have anew feature which we have added a link to in the top link but it is only available to certain customers.

We have declared the link an xml file, and shows up perfectly i just now need to know if it is possible to hide the link if a customer is not logged in or is logged in but is not in a certain group

<reference name="top.links">
                <block type="advancedquickorder/index" name="add.advancedquickorder.link">
                    <action method="addAdvancedquickorderLink" ifconfig="advancedquickorder/general/active"><param>top</param></action>              
                </block>
            </reference> 

Best Answer

Add the below codes, In config.xml

   <frontend>     
      <events>
        <controller_action_layout_generate_xml_before>
           <observers>
              <Mymodule>
                 <class>Namespace_Mymodule_Model_Observer</class>
                 <method>addmyblock</method>
              </Mymodule>
           </observers>
         </controller_action_layout_generate_xml_before>
       </events>
    </frontend> 

In observer.php

class Namespace_Mymodule_Model_Observer
{

    public function addmyblock(Varien_Event_Observer $observer){

        $loggedIn = Mage::getSingleton('customer/session')->isLoggedIn();
        // add Handler when customer is loggedin
        if($loggedIn){
            $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId(); 
            $groupname = Mage::getModel('customer/group')->load($groupId)->getCustomerGroupCode();
            if($groupname == "group name"){
                $layout = $observer->getLayout();
                $layout->getUpdate()->addUpdate('<reference name="top.links">
                                <remove name="add.advancedquickorder.link"/></reference>');              
                $layout->generateXml(); 
            }
        }
    } 
}

You can remove any of the top links with the above code

Related Topic