I need to find a way to enhance the admin products grid with capability to fill in the SKU field more than one reference.
For example, get copy/paste from an Excel list multiple sku and get the matching list on Magento.
I've already a solution in place based on default grid block rewriting but there is a conflict with a new cleaner extension I want to use with (Enhanced Admin Grids from Magento connect).
What is the best way to achieve this? Rewriting how the default sku field is working or adding a new custom one?
Best Answer
In my opinion adding a new custom clumn is better because of two reasons:
The core team may modify the existing
sku
column any time. Additionally, as you said any extension developer may rewrite this grid or more specific this column given it's a very basic one. If you create your own column probably nobody else knows about it what makes it less likely that somebody will screw around with your columnIt's hard (if not impossible) to modify the existing columns without rewrites or observer a very generic event and therefore possibly slowing down the page generation.
Having said that and contra-dicting myself there is a way to use a product-grid-specific event and modify the original
sku
column. You should document this though as it's not very intuitive if you find the code again two years later.The solution is: observe the event
adminhtml_catalog_product_grid_prepare_massaction
amd manipulate the already existing column.I'll assume that you created an extension before so I'll skip Magento extension creation basics. I call my example extension
Emzee_MultiSkuFilter
.Add an observer for
adminhtml_catalog_product_grid_prepare_massaction
inapp/code/local/Emzee/MultiSkuFilter/etc/config.xml
:In the observer class
app/code/local/Emzee/MultiSkuFilter/Model/Observer.php
you receive the grid block. Find thesku
column and define a filter condition callback which will be used to query the database when you filter bysku
:In the filter callback
app/code/local/Emzee/MultipleSkuFilter/Model/Resource/Filter.php
, split up the SKUs and construct your SQL. It's not the most elegant code but it works. By default the SKUs have to be an exact match but you also can search for%test%
to use wildcards.This is what the result looks like: