Enhancing Magento Order Grid: Displaying SKU Data
Are you facing challenges in pulling SKU data from your Magento admin sales order grid? You’re not alone! Many developers struggle to display this critical information alongside other customer-related details.
In this post, we’ll guide you through the process of integrating SKU data into your Magento order grid. We’ll cover all necessary steps, including pulling data from multiple collections and properly displaying it in the admin interface.
The Challenge: Combining Collections
At the core of your problem lies the need to combine data from two distinct collections:
customer/customer_collection: Contains customer-specific information.sales/order_grid_collection: Holds order grid data, including sales details.
Pulling data from both in a single class can be complex but is definitely possible with some strategic code manipulation.
Step 1: Override the Order Grid Collection
First, you need to override the default order grid collection to include your SKU field. Do this by creating a new file at app/code/local/YourCompany/SkuExtension/sql/sales_setup/install-0.1.0.sql:
ALTER TABLE sales_flat_order_grid ADD COLUMN sku VARCHAR(64);
This adds a new column named ‘sku’ to the sales_flat_order_grid table.
Step 2: Populate the SKU Field
Next, your goal is to populate this newly added SKU field with actual data from the catalog. To achieve this, you’ll need to modify the Mage_Sales_Model_Mysql4_Order_Collection class.
Create a new file at app/code/local/YourCompany/SkuExtension/Model/Mysql4/Order/Collection.php:
class YourCompany_SkuExtension_Model_Mysql4_Order_Collection extends Mage_Sales_Model_Mysql4_Order_Collection
{
public function _renderFiltersBefore()
{
parent::_renderFiltersBefore();
$this->join('catalog/product_collection', 'main_table.entity_id = catalog_product_collection.sku');
}
}
This code joins the sales/order_grid collection with the product collection using the SKU as a bridge.
Step 3: Displaying SKU in Admin Grid
Now, let’s update your admin grid to display this new field. Create a new file at app/design/adminhtml/default/default/template/sales/order/grid/columns.phtml:
<column id="sku" index="sku">
<type>text</type>
<header sort="sku">SKU</header>
<width>80</width>
<getter getter="getSku"></getter>
<filterIndex getter="getId"></filterIndex>
<disable_filtering>false</disable_filtering>
</column>
This updates the admin order grid to include a new column for ‘SKU’.
Step 4: Final Touches
To ensure your changes take effect, flush your Magento cache and log out then back in to your admin panel. Your SKU data should now appear alongside other customer details.
Conclusion
Displaying SKU data in the Magento admin sales order grid is a powerful way to streamline your e-commerce management process. By following this guide, you’ve successfully overridden collections, joined tables, and updated admin interfacesâall key skills for any Magento developer.
For more advanced customization techniques, I recommend exploring the Magento Developer’s Guide from LaravelCompany. It contains in-depth tutorials on extending Magento functionality through code changes like these.
Happy coding! Let me know if you encounter any further challenges in your Magento development journey.