控制器:

function downProductAction(){
    $filename = 'product.csv';
    $content = Mage::helper('module')->getProductList();
    $this->_prepareDownloadResponse($filename, $content);
}

Blcok:

function _getCsvHeaders($products)
{
    $product = current($products);
    $headers = array_keys($product->getData());
    return $headers;
}

Helper:

function getProductList() 
{
    $collection = Mage::getModel("catalog/product")->getCollection();
        if(!is_null($collection)){
               $items = $collection->getItems();
               if (count($items) > 0) {
                     $io = new Varien_Io_File();
                     $path = Mage::getBaseDir('var') . DS . 'export' . DS;
                     $name = md5(microtime());
                     $file = $path . DS . $name . '.csv';
                     $io->setAllowCreateFolders(true);
                     $io->open(array('path' => $path));
                     $io->streamOpen($file, 'w+');
                     $io->streamLock(true);

                     $io->streamWriteCsv(array('sku','name'));
                     foreach ($items as $product) {
                            $io->streamWriteCsv(array($product->getSku(),$product->getName()));
                     }
                     return array(
                           'type' => 'filename',
                           'value' => $file,
                           'rm' => true
                     );
               }
       }
}

results matching ""

    No results matching ""