0

I want made a cronjob to export an colletion to csv for ciao and kelkoo comparative of prices.

Where i can see a example in the core of Magento??

I see in the next location:

Mage/Dataflow/Model/Batch/export.php

but this file don´t find nothing.

maybe can see how to work in :

Mage/importexport/

1 Answer 1

4

Have a look at Mage_Adminhtml_Block_Widget_Grid::getCsv() and Mage_Adminhtml_Block_Widget_Grid::getCsvFile() as an example.
Here is the relevant portion of the first as an example (comments added by me).

public function getCsv()
{
    $csv = '';
    $this->_isExport = true;
    $this->_prepareGrid(); // add the attributes to load, maybe required filters
    $this->getCollection()->getSelect()->limit(); // only unique records
    $this->getCollection()->setPageSize(0); // no paging, all records matching the set filters
    $this->getCollection()->load();
    $this->_afterLoadCollection(); // load additional data on the collection items if needed

    $data = array();
    // This foreach block adds headers to the columns
    foreach ($this->_columns as $column) {
        if (!$column->getIsSystem()) {
            $data[] = '"'.$column->getExportHeader().'"';
        }
    }
    $csv.= implode(',', $data)."\n";

    // $column is an instance of Mage_Adminhtml_Block_Widget_Grid_Column
    // Just a wrapper for getting the values from the collection items
    foreach ($this->getCollection() as $item) {
        $data = array();

        foreach ($this->_columns as $column) {
            if (!$column->getIsSystem()) {
                $data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'),
                    $column->getRowFieldExport($item)) . '"';
            }
        }
        $csv.= implode(',', $data)."\n";
    }

    // Grid totals are only used by reports
    if ($this->getCountTotals())
    {
        $data = array();
        foreach ($this->_columns as $column) {
            if (!$column->getIsSystem()) {
                $data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'),
                    $column->getRowFieldExport($this->getTotals())) . '"';
            }
        }
        $csv.= implode(',', $data)."\n";
    }

    return $csv;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.