views:

33

answers:

1

Hi,

The import products profile allows you to filter which products to export by name, sku... I want to have the same functionality in the manage products page. The admin will filter products in the grid and then click a "export" button to get the filtered products.

How can I add the "export" button? what template/block do I need to override? When the admin click the button, how can I get the filtered collection? How can I export the filtered collection to a csv file? Can I use the dataflow for that?

Thanks

A: 

You will need to implement a new massAction in the admin product controller. A good way to start is to have a look at the product controller and see how the other massActions are implemented.

Let's have a look... First you will need to add a declaration of a massAction to a grid. This can be done in app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php. You need to add the following in the _prepareMassaction method:

$this->getMassactionBlock()->addItem('export', array(
                'label' => Mage::helper('catalog')->__('Export to CSV'),
                'url'   => $this->getUrl('*/*/massExport', array('_current'=>true)),
            ));

Now you will have to implement this new action in the product controller (app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php):

public function massExportAction()
    {
        $productIds = $this->getRequest()->getParam('product');
        if (!is_array($productIds)) {
            $this->_getSession()->addError($this->__('Please select product(s).'));
            $this->_redirect('*/*/index');
        }
        else {
            //write headers to the csv file
            $content = "id,name,url,sku\n";
            try {
                foreach ($productIds as $productId) {
                    $product = Mage::getSingleton('catalog/product')->load($productId);
                    $content .= "\"{$product->getId()}\",\"{$product->getName()}\",\"{$product->getProductUrl()}\",\"{$product->getSku()}\"\n";
                }
            } catch (Exception $e) {
                $this->_getSession()->addError($e->getMessage());
                $this->_redirect('*/*/index');
            }
            $this->_prepareDownloadResponse('export.csv', $content, 'text/csv');
        }

    }

The code is mostly copied from the massDeleteAction, but instead of deleting products you should add to the $content variable. After you are done creating the content of your csv export (you will probably need to add other fields to it) you need to call the _prepareDownloadResponse method of the controller class. That's it, you have your custom export in place!

On a final note, once you are happy with the changes remember to move them to your local code pool so that your magento installation remains update-proof :)

silvo
Where is the admin product controller?I can't see massAction in Mage_Adminhtml_Catalog_ProductControllerIf you can find code it will be great. thanks
pablo
@Pablo: I have ammended my response. Please see above for a working code sample...
silvo
Instead of creating the csv myself, is it possible to pass the product ids or filters to the dataflow to create the csv? Is it possible to add a category filter to the grid?
pablo
@Pablo: I don't know about using the dataflow. I have not done anything with it yet. Maybe someone else will be able to comment.
silvo