Observer for Admin Order Grid
-
16-10-2019 - |
Question
I would like to restrict order based on an Admin user. So for example the admin user would have an attribute that is assigned to a certain region (Group of Zipcodes). When the user logs in they could only see the orders for their region.
I am pretty clear on everything except the event to show the grid. I would like to accomplish this without overriding the core code.
Solution
Here's a way - though I'm not terribly excited about it. Maybe someone has a better thought:
To avoid potential issues with your admin panel causing locks on your sales tables (think sales_flat_order_address
) I would add appropriate columns for filter to the sales_flat_order_grid
table that have your billing region. You don't have to display these with an addColumn
- they'll only be used for applying a default filter to the collection.
Using the event sales_order_grid_collection_load_before
:
class Yc_Ym_Model_Observer
{
public function gridCollectionLoadBefore($observer)
{
$allowedRegionId = Mage::getSingleton('admin/session')->getAllowedRegionId();
$collection = $observer->getEvent()->getOrderGridCollection();
$collection->addFieldToFilter('billing_region',array('eq'=>$allowedRegionId));
}
}
This allows you to filter the collection by default. You could skip this entire block conditionally (for instance, super admins who can view all regions) by doing a Mage::getSingleton('admin/session')->isAllowed()
which, in essence, does an ACL check to skip this filter application. You would be responsible for setting this ACL.
OTHER TIPS
I would recommend observing for loading of Mage_Sales_Model_Resource_Order_Collection and on before load event add your filters. sales_order_collection_load_before
should be the event to look at.
Try with this event admin_session_user_login_success
For further usefulness refer the following urls.
http://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/magento_events http://www.nicksays.co.uk/magento-events-cheat-sheet-1-7/