Magento – Observer for Admin Order Grid

adminhtmlcollection;event-observer

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.

Best Answer

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.

Related Topic