- Canadian eCommerce (91)
- Canadian eCommerce Benchmark Quarterly Reports (5)
- Canadian eCommerce Monthly Trends (20)
- Careers (5)
- Design & User Experience (31)
- eCommerce (191)
- eCommerce Toronto Meetup (23)
- Email Marketing (1)
- Inbound Marketing (10)
- Infographics (46)
- Integration (1)
- Magento Commerce (89)
- Magento Extensions Updates (11)
- Mobile (11)
- Press Releases (1)
- Project Management (9)
- Ramblings (40)
- Search Engine Marketing (12)
- Search Engine Optimization (11)
- Site Launches (7)
- Social Media (16)
- This Week in eCommerce (14)
- This Week in eCommerce: May 13 – May 17
- 5 Ways Tablets & Mobile Devices Influence Design Trends
- [Infographic Wednesday] – 10 Ways to Increase Online Sales
- The Basics of Integration between Business Vision Essentials and Magento in C#
- New Site Launch: Books for Christ
- Email Marketing: It’s Personal (or at least it should be)
- [Infographic Wednesday] – Are You Ready For The Digital Revolution?
- April 2013 – Canadian eCommerce Monthly Trends Report
- #mornin! http://t.co/XxOfU2i4EM
- Good to be home. #toronto
- Just posted a photo http://t.co/kXx9RxGdpA
- Every week we gather some of the top articles in #eCommerce to share with our followers. Here is our latest post http://t.co/3RzFVpNWkQ
- @demacmedia @ Happy to connect you
- @demacmedia office #security gives zero "crab" about whether the office is secured... Lol http://t.co/XSi8ldDt29
- Every week we gather some of the top articles in #eCommerce to share with our followers. Here is our latest post http://t.co/YPzpufg9xW
- So excited to have you on our team! RT @n_forten: This is what internships are all about. Week 1 @demacmedia & I'm already #learning so much
[Mini Tutorial] – Adding Custom Attributes to the Backend Product Grid
In most cases the default Magento Product Grid under Catalog > Manage Products will display all the required information for easily filtering and searching the products, however in some particular cases store owners might want to be able to display and filter the catalog product grid by using non default attributes.
In this mini tutorial we will over a quick method for adding additional attributes to our grid and displaying the option filters correctly. For this tutorial we will use the color and manufacturer attributes as examples.
After creating our base Module structure we need to rewrite the Adminhtml Catalog Product Grid in order for properly manipulate the collection and the grid columns:
<config> <modules> <Demac_Grid> <version>0.1.0</version> </Demac_Grid> </modules> <global> <blocks> <adminhtml> <rewrite> <catalog_product>Demac_Grid_Block_Grid</catalog_product> </rewrite> </adminhtml> </blocks> </global> </config>
Now that our rewrites are in place let’s copy of the Product Grid Block under our Module Block folder; the 2 functions that we will need to change are _prepareCollection and _prepareColumns.
_prepareCollection is in charge of setting up our collection and by default it only loads the necessary attribute for populating each column.
protected function _prepareCollection() { $store = $this->_getStore(); $collection = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('sku') ->addAttributeToSelect('name') ->addAttributeToSelect('attribute_set_id') ->addAttributeToSelect('type_id'); // Add our custom attributes $collection->addAttributeToSelect('color') ->addAttributeToSelect('manufacturer'); if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) { $collection->joinField('qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left'); } if ($store->getId()) { $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID; $collection->addStoreFilter($store); $collection->joinAttribute('name', 'catalog_product/name', 'entity_id', null, 'inner', $adminStore); $collection->joinAttribute('custom_name', 'catalog_product/name', 'entity_id', null, 'inner', $store->getId()); $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner', $store->getId()); $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner', $store->getId()); $collection->joinAttribute('price', 'catalog_product/price', 'entity_id', null, 'left', $store->getId()); } else { $collection->addAttributeToSelect('price'); $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner'); $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner'); } $this->setCollection($collection); parent::_prepareCollection(); $this->getCollection()->addWebsiteNamesToResult(); return $this; }
Now that our custom attributes are being loaded as part of our collection data we can add the corresponding columns to the _prepareColumns function.
This can easily achieved by using the Mage_Adminhtml_Block_Widget_Grid::addColumn function:
$this->addColumn('attribute_code', array( 'header'=> Mage::helper('catalog')->__('Attrib. Name'), 'width' => '100px', 'index' => 'attribute_code', 'type' => 'options', 'options' => $attribute_options, ));
As we can see the addColumn function takes 2 parameters:
- A unique ID for the column, in this case our attribute code
- An array with the following values:
- Header: The title for the column.
- Width: The width value of the column in pixels
- Type: The data type of the column(number, currency, options), this will take care of the formatting for our column values
- Options: Allows to pre-populated the filter dropdown and is used to display correctly the values of our column
- Index: is the matching attribute code from our collection
Now if we check our category grid in the Magento backend, we can see that the columns appear but they are empty this is because we haven’t told Magento where to get the option values for our columns yet.
So let’s go back to our Grid class and create the a new protected function:
protected function _getAttributeOptions($attribute_code) { $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attribute_code); $options = array(); foreach( $attribute->getSource()->getAllOptions(true, true) as $option ) { $options[$option['value']] = $option['label']; } return $options; }
And now lets update our addColumn functions to the following:
$this->addColumn('attribute_code', array( 'header'=> Mage::helper('catalog')->__('Attrib. Name'), 'width' => '100px', 'index' => 'attribute_code', 'type' => 'options', 'options' => $this->_getAttributeOptions('attribute_code'), ));







