Magento: How to add a link to your account dashboard depending on customer group

8 August 2016| Post by Graham

AuthorGraham

In a recent project I was tasked with adding links to the account dashboard depending on your customer group.

Here is how I achieved it:

Step 1. Define the module

app/etc/modules/Limely_AccountLink.xml

				




true
local



This file registers the Limely_AccountLink module into Magento’s architecture.

Step 2. Initialise the module configuration

app/code/local/Limely/AccountLink/etc/config.xml

				




0.0.1





Limely_AccountLink_Model








singleton
accountlink/observer
addLink






This file defines the module within Magento, in this instance it is used to define the “accountlink” model namespace, it also attaches an event listener to the “controller_action_layout_generate_xml_before” event.

Step 3. Create the event listener

app/code/local/Limely/AccountLink/Model/Observer.php

				

isLoggedIn()) {
return;
}

$customer = $helper->getCustomer();

if (!$customer->getGroupId() !== 5) {
return;
}

$update = $observer->getLayout()->getUpdate();

if (in_array(‘customer_account’, $update->getHandles())) {
$update->addUpdate(‘


mylink mylink


‘);
}
}
}

This file is the Observer that I’ve created to listen to any events defined in the “app/code/local/Limely/AccountLink/etc/config.xml” file. The logic in the addLink method is as follows :

  1. Check the user is logged in, if not then stop
  2. Check the user belongs to a certain user group, if not then stop.
  3. Check that the current request relates to the account area, if so then use the Mage_Core_Model_Layout_Update ($update) instance to dynamically add a layout update to the “customer_account_navigation” reference.
Scroll