0

I have the code below and I am wondering where to put this code/how to run it then to get a .csv file or .txt file at the end of the day.

$path = Mage::getBaseDir() . DS.'var'.DS ;
$name ='sp1';
$file = $path . DS . $name . '.csv';
$csv = new Varien_File_Csv();
$csvdata = array();         

/* set Header  row*/
$headerRow = array(
        'sku',
        'mysku',
);
$csvdata[]=$headerRow;

/* add product data here */

 $productCollection=Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('sku')->addAttributeToFilter('sku', array(
       'like' => 'A%'));

     $productCollection->getSelect();   
    $write = Mage::getSingleton("core/resource")->getConnection("core_write");
    $requestPathField = new Zend_Db_Expr($write->quoteIdentifier('sku'));
     $write->getSubstringSql($requestPathField,1);

    echo $productCollection->getSelect()->columns(array('mysku' =>  $write->getSubstringSql($requestPathField,1)));
    foreach($productCollection as $product):
    echo $product->getMysku().'<br/>';
    endforeach;


foreach($productCollection as &$product){
    $csvdata[]=array($product->getSku,$product->getMysku());
}

$csv->saveData($file , $csvdata);

UPDATE

So like this it would write my csv file? My initial question was still where do I call it (where does this code stand) and where is the output. Will it be in media/export ?

2
  • ok i will check ..
    – Amit Bera
    Commented Feb 27, 2015 at 11:05
  • thomas,it download at magentodir/var/sp1.csv
    – Amit Bera
    Commented Feb 27, 2015 at 14:24

2 Answers 2

2

You can do this by Varien_File_Csv class which create csv.

Please check

$path = Mage::getBaseDir() . DS.'var'.DS ;
$name ='sp1';
$file = $path . DS . $name . '.csv';
$csv = new Varien_File_Csv();
$csvdata = array();         

/* set Header  row*/
$headerRow = array(
        'sku',
        'mysku',
);
$csvdata[]=$headerRow;

/* add product data here */
foreach($productCollection as &$product){
    $csvdata[]=array($product->getSku,$product->getMysku());
}
$csv->saveData($file , $csvdata);
0
0
    <?php  
use Magento\Framework\App\Bootstrap;
include('./app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
 
$objectManager = Magento\Framework\App\ObjectManager::getInstance();
getAllProducts($objectManager);

function getAllProducts($objectManager) {

$output = fopen('php://output', 'w');

// output the column headings
// fputcsv($output, array('id', 'sku', 'description', 'title', 'meta_description'));

$header[]=array('id', 'sku','name');

    $objectManager->get('Magento\Framework\Registry')->register('isSecureArea', true);
    $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    $collection = $productCollection->create()->addAttributeToSelect('*')->load();
    $app_state = $objectManager->get('\Magento\Framework\App\State');
    $app_state->setAreaCode('frontend');

    foreach ($collection as $product){
        $id=$product->getId();
        echo $id.PHP_EOL;
        $sku=$product->getSku();
        echo $sku.PHP_EOL;

        $name=$product->getName();

        echo $name.PHP_EOL;

        $header[]=array($id, $sku, $name);
    // fputcsv($output, array($id, $name));
    }
    $fp = fopen("getProducts.csv","w"); //first the read the file and header and create the data in csv
// $fp = fopen('php://output', 'wb');
    foreach ($header as $line) {
        fputcsv($fp, $line);
    }
    fclose($fp);      
}
?>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.