Magento: Add attribute columns in ‘Manage Products’ grid

Magento does not provide us with the ability to choose which attributes are included as columns in the Manage Products grid but it’s fairly simple to make the necessary code changes.

The code that generates the Manage Products grid is at /app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php. The first thing you need to do is copy Grid.php into to the local directory structure. In other words, you copy Grid.php into the following location; /app/code/local/Mage/Adminhtml/Block/Catalog/Product/. If there is no such location, then you must create the necessary directories. The final location of the file must be; /app/code/local/Mage/Adminhtml/Block/Catalog/Product/Grid.php

Now, open Grid.php (the one in the local directory structure) and begin editing. Find the following code;


$this->addColumn('sku',
	array(
		'header'=> Mage::helper('catalog')->__('SKU'),
        'width' => '80px',
        'index' => 'sku',
    ));

That’s the code that adds the SKU column to the product grid.  Now, let’s say you have a custom attribute called Supplier ID (supplier_ID) and you want these to appear on the Manage Products grid as well. Place the following code either before or after the above block of code, as long as it’s inside _prepareColumns().


$this->addColumn('supplier_id',
	array(
		'header'=> Mage::helper('catalog')->__('Supplier ID'),
		'width' => '150px',
		'index' => 'supplier_id',
	));

Then add the following line to _prepareCollection() where the other attributes are listed like this;


->addAttributeToSelect('supplier_id')

That should be all you need to do. You might have to re-compile, refresh your caches, logout, and log back in to see the change in your product grid.

The above example is for adding an attribute with a Catalog Input Type for Store Owner of Text Field. What if your attribute uses a dropdown list? The code above will have to be modified.

Let’s say you have an attribute called Supplier (supplier) which in the product editor presents a dropdown list of suppliers to choose from. To do this, we can add the following code to _prepareColumns():


        $supplier_items = Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter()->join('attribute','attribute.attribute_id=main_table.attribute_id', 'attribute_code');
        foreach ($supplier_items as $supplier_item) :
            if ($supplier_item->getAttributeCode() == 'supplier')
                $supplier_options[$supplier_item->getOptionId()] = $supplier_item->getValue();
        endforeach;

        $this->addColumn('supplier',
            array(
                'header'=> Mage::helper('catalog')->__('supplier'),
                'width' => '150px',
                'type'  => 'options',
                'index' => 'supplier',
                'options' => $supplier_options,
        ));

And let’s not forget to add the following line to _prepareCollection() where the other attributes are listed like this;


->addAttributeToSelect('supplier')

That should do it for you. Re-compile, refresh your caches, and logout and then back in if you need to.

Magento: Programmatically Updating SKUs in Bulk

Magento does not make it easy to change SKUs in bulk. Try to do it via the import/export or dataflow profiles and you run into all kinds of problems. There is, however, a fairly easy way to do it and it simply involves adding a php script to your server and a CSV file of before and after SKUs. Here’s the step-by-step;

Note: Be sure to test this with only one or two product SKUs before doing it with all your SKUs. Also, be sure to backup your database before attempting this.

1. Create a CSV File with Before and After SKUs

In the first column, list your current SKUs and in the second column list the new SKUs.

Do not include headings in your CSV file.

Be sure this file is saved as a CSV file in the UTF-8 or ANSI encoding. You might run into problems with this if you create the file using Excel.

2. Put the CSV File on Your Server

Upload the CSV file to the var/export directory on your Magento server so that it’s path is /var/export/sku2sku.csv.

3. Create the PHP Script

On your server, in your Magento installation directory (the one where you see the app, var, skin, media, js and other directories), create a new file, save it, and name it “updateskus.php”.

Paste the following php code into updateskus.php and save the file.

<?php

include_once './app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$updates_file="./var/export/sku2sku.csv";
$sku_entry=array();
$updates_handle=fopen($updates_file, 'r');
if($updates_handle) {
while($sku_entry=fgetcsv($updates_handle, 1000, ",")) {
$old_sku=$sku_entry[0];
$new_sku=$sku_entry[1];
echo "<br>Updating ".$old_sku." to ".$new_sku." - ";
try {
$get_item = Mage::getModel('catalog/product')->loadByAttribute('sku', $old_sku);
if ($get_item) {
$get_item->setSku($new_sku)->save();
echo "successful";
} else {
echo "item not found";
}
} catch (Exception $e) {
echo "Cannot retrieve products from Magento: ".$e->getMessage()."<br>";
return;
}
}
}
fclose($updates_handle);
?>

4. Run the Script

To run the script simply use your internet browser and navigate to http://yoursite.com/updateskus.php. If you have a multi-site setup use the master or primary site as set by your hosting provider.

When the page opens you should see confirmation messages that your SKUs were updated. Your SKUs should now be successfully updated.

If you’re finished updating the SKUs, remove the CSV and PHP files that you added to the server.

5. Errors

If you run into the following error, don’t worry too much. Just re-run the script and see if more SKUs get updated.

Cannot retrieve products from Magento: SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction

If you have a lot of SKUs to update you can expect the script to take several minutes at least, to complete.

This method was tested on a multi-site Magento Community 1.6.1 installation.

This method is based on this Magento Commerce article.

(Update Sep. 20, 2013) One of the readers noted that the script modified the ‘Main Website’ store view. When this happened, it resulted in  products no longer using the ‘default’ data such description, name, etc. The reader suggested replacing the line “Mage::app();” with “Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);”. I haven’t tested it myself, but it looks like it should work, so I updated the code shown above. Thanks, Andrew!