2

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

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

  • I took the group name bit out and just done the IF statement from the groupID – Adam Allen Jun 8 '15 at 14:55
1

You can do this by create handler according to Customer group using magento/event observer.

On controller_action_layout_load_before event fire an observer which will insert a new handler on current layout.

This layout name should be dynamic basic of Customer group name.

If a general group customer is loggin then it handler anme is customer_group_general

Logic like customer_group_CustomerGROUPCODE.

Config.xml code is : `

       <frontend>
        <events>
            <controller_action_layout_load_before>
                <observers>
                    <customer_group_wise_handler>
                        <class>stackexchange70064/observer</class>
                        <method>addCustomerLoadLayout</method>
                    </customer_group_wise_handler>
                </observers>
            </controller_action_layout_load_before>
       </events>
 </frontend>

`

Observer code:

<?php
class Magento_Stackexchange70064_Model_Observer{

    public function addCustomerLoadLayout($observer)
    {
        $loggedIn = Mage::getSingleton('customer/session')->isLoggedIn();
        // add Handler when customer is loggedin
        if($loggedIn):

        $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
        //Get customer Group name

        $group = Mage::getModel('customer/group')->load($groupId);

        // add handler to current layout
        $observer->getEvent()->getLayout()->getUpdate()
        ->addHandle('customer_group_'.strtolower($group->getData('customer_group_code')));
        //Mage::log('customer_group_'.strtolower($group->getData('customer_group_code')), null, 'logfile.log');
        endif;

    }
}

You can full modules at Github

Then those handler and using layout tag remove attribute remove the a toplink for a particular customer grop

For example:

suppose want to remove add.advancedquickorder.link block for general Customer group.

Then try this

<customer_group_general>
   <reference name="top.links"><remove name="add.advancedquickorder.link"/></reference>
</customer_group_gerenal>
  • I implemented your solution but it didnt seem to work, i just wanted to check with you that i was implementing it correct. I created a directory in app/code/local/CGroup/CGroup/etc and Model and added your config code to the app/code/local/CGroup/CGroup/etc and added the observe code to the app/code/local/CGroup/CGroup/Model but changing the class to class CGroup_CGroup_Model_Observer. Is that correct or am i mssing something? – Adam Allen Jun 5 '15 at 9:43
  • There are issue xml , i have update code – Amit Bera Jun 6 '15 at 4:52
  • I used all the files from Github but it still doesnt work – Adam Allen Jun 8 '15 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.