Modifying the Magento Admin Panel can be an intimidating task. Recently, we were asked to come up with a way of displaying a list of customers for products purchased. We had devised some custom functionality for displaying on a product, within the admin panel, a list of customers who had purchased said product. This information was to be gathered and displayed only for certain products belonging to a particular attribute set.
In tackling this exercise, I added a grid display to a newly added tab on the product view of the admin panel to output this list of customers. It was fully functional with sorting and filtering through Ajax, and included the ability for CSV export and some mass actions.
Outside of this greater functionality, I feel the ability to view relevant customer order information while viewing a product is a valuable asset, providing relevant metrics at a glance for storeowners and customer service representatives. Moreover, developers can adapt this technique to display a grid containing any relevant collection of information on to any form tab of the Magento admin panel desired.
In this tutorial, I will be demonstrating how to implement this functionality of displaying a customer order grid on a product tab. The final module is included at the end of this post for your convenience.
There are a few important functions here to note. _prepareCollection(), as you might correctly presume, prepares the grid collection object. Here we are joining a collection of order items with the orders table so that we may obtain the order’s increment ID, status, currency code, and customer information. All of which will be used in the display of the custom order grid. _prepareColumns() sets the columns and headings of that grid.
The getRowUrl() function sets the link for each row in the grid to send the end user to the order view page for the order in question.
In the __construct(), we are enabling the grid to support Ajax. Note the ID set here is consistent with the fallback URL specified in the getGridUrl() function of this same class.
Our module’s config.xml file specifies an adminhtml layout file at demac/productorders.xml. This layout sets the block to use during some of our Ajax actions. This is the same block class we just discussed.
Similarly, in our extension of the Adminhtml ProductController, our two methods productordersAction() and productordersGridAction() get the block from the layout we have just defined above. Our controller also includes an exportCsvAction() triggered on press of the button above the grid. It makes use of the getCsvFile() method from the Mage_Adminhtml_Block_Widget_Grid, which Demac_ProductOrders_Block_Adminhtml_Catalog_Product_Edit_Tab_Orders extends.
If we then view Aviator Sunglasses through the admin panel, we will see a new “Orders” tab on the left-hand menu. This tab will display our newly created order grid as follows:
Here, we can see that Linda Wood’s order is displayed with its quantity ordered and subtotal as described above. We can see, at a glance, the status of the order, the currency used, the date of purchase, the store it was purchased from, and the order number. Above, you will notice our CSV Export feature as well. In addition, if the row for Linda’s order is clicked, we will be directed to the full order information page as depicted earlier.
Once this is all in place, the order tab will appear and propagate for all products and orders both existing and new.
You can download the full Demac_ProductOrders module for yourself, to use or adapt, here.
For more Magento resources, download our free eBook containing 25 Real-World Examples of Magento Shopping Cart Price Rules