0

Is there a way that I can export all products on a certain category via CSV or excel file?

0

4 Answers 4

0

Create one file on your magento root directory and add below code in that file.

<?php

error_reporting(E_ALL | E_STRICT);

define('MAGENTO_ROOT', getcwd());

$mageFilename = MAGENTO_ROOT . '/app/Mage.php';

require_once $mageFilename;

Mage::setIsDeveloperMode(true);

ini_set('display_errors', 1);

Mage::app();

$categoryId = 14; // category Id for which you want products

$category = Mage::getModel("catalog/category")->load($categoryId);

$products = Mage::getModel("catalog/product")->getCollection();
$products->addAttributeToSelect("*");
$products->addCategoryFilter($category);

$fileName = 'exports_'.$category->getId().'.csv';

$fp = fopen($fileName, 'w');

$csvHeader = array("sku", "name"); // you can specify more fields which you need in export file

fputcsv($fp, $csvHeader, ",");

foreach ($products as $product) {

    $sku = $product->getSku();

    $name = $product->getName();

    fputcsv($fp, array($sku, $name), ",");
}

fclose($fp);
?>

If you have permission issue on magento root you can export in var or media folder as well.

0

You can do it programmatically by using following code:

<?php  
    error_reporting(E_ALL | E_STRICT);
    $mageFilename = 'app/Mage.php';
    require_once $mageFilename;
    Mage::getIsDeveloperMode(true);
    ini_get('display_errors', 1);
    ini_get('memory_limit', '600M');
    ini_get('max_execution_time', 1800);
    umask(0);
    Mage::app('admin'); 
    $fileLocation = Mage::getBaseDir('media').DS.'productlist.csv' ;//file path of 
    $mageCsv = new Varien_File_Csv();
    $categoryId = 46; //put your cat id here
    $productsModel =  Mage::getModel('catalog/category')->load($categoryId)
                        ->getProductCollection()
                        ->addAttributeToSelect('*') 
                        ->addAttributeToFilter('status', 1) 
                        ->addAttributeToFilter('visibility', 4) ;
    $productsRow = array();
    foreach ($productsModel  $product)
    {
        $data = array();

        $data['id']         = $product->getId();
        $data['name']       = $product->getName();
        $data['sku']        = $product->getSku();
        $data['weight']     = $product->getWeight();
        $data['urlkey']     = $product->getUrlKey();
        $data['price']      = $product->getPrice();
        $data['metatitle']  = $product->getMetaTitle();
        $data['metakey']    = $product->getMetaKeyword();
        $data['metadesc']   = $product->getMetaDescription();
        $data['disc']       = $product->getDescription();
        $data['sdic']       = $product->getShortDescription();
        // you other product attribute goes here

        $productsRow[] = $data;
    }   
    $mageCsv->saveData($fileLocation, $productsRow); 
    echo 'done!'
    ?>

You need to put this file on your root folder and run via url, when script complete the process check your media folder.

0

here is my version too:

// Avoid any time limit
set_time_limit(0);

// Avoid any memory limit
ini_set('memory_limit', -1);

// Include bootstrap code and Mage class
require_once 'app/Mage.php';

// Enable developer mode
Mage::setIsDeveloperMode(true);

// Set the default file creation mask
umask(0);

// Init application with default store
Mage::app();

// Init admin store
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

// Load adminhtml config area
Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_ADMINHTML);

// Initialize administrator session
$userModel = Mage::getModel('admin/user');
$administrator = $userModel->load(1); // change administrator id here
$adminSession = Mage::getSingleton('admin/session');
$adminSession->renewSession();
$adminSession->setUser($administrator);
$acl = Mage::getResourceModel('admin/acl')->loadAcl();
$adminSession->setAcl($acl);

/** @var Mage_Catalog_Model_Category $category */
$category = Mage::getModel('catalog/category')->load(4);

/** @var Mage_Catalog_Model_Resource_Product_Collection */
$products = $category
    ->getProductCollection()
    ->addAttributeToSelect('name'); // add needed attributes

$csvColumns = array('entity_id', 'sku', 'type_id', 'name');

$csvData = array();
foreach ($products as $product) {
    $csvData[] = array_map(
        function($column) use ($product) {
            return $product->getData($column);
        },
        $csvColumns
    );
}

$csv = new Varien_File_Csv();
$csv->saveData('/tmp/products.csv', array($csvColumns) + $csvData);

Hope it helps

0

Create a folder in your root directory and upload the getproductlist.php. Inside the directory create a sub directory called csv.

Below is the code for getproductlist.php

<?php
require_once '../app/Mage.php';
umask(0);
Mage::app('default');
function array2csv(array &$array, $name_extension=null) {

    if (count($array) == 0) {return null;} 

    $name_extension = date('m-d-Y', strtotime("yesterday"))."_".time();

    $FileName = "csv/Product_list"."-".$name_extension.".csv";
    echo "Download as CSV file: <a href='$FileName'>Download</a>";

    $df = fopen($FileName, 'w');
    $titles = array();


    array_push($titles, "id", "name", "sku", "urlkey", "price", "metatitle", "metakey", "metadesc", "disc", "sdic");
    $blankArray = array("","","","","","","","","","");
    fputcsv($df, $titles);


    foreach ($array as $row) {
        fputcsv($df, $row);
    }

    fclose($df);

}//END OF Function array2csv

$collection = Mage::getModel('catalog/product')
                ->getCollection()
                ->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('category_id', 16);
                //Give the relevant category id which you want to import.
$m=0;
$itsmArr = array();
foreach ($collection as $product)   {
 $prod_id = $product->getId();
 $_product  = Mage::getModel('catalog/product')->load($product->getId());   

    $prd['id']         = $_product->getId();
    $prd['name']       = $_product->getName();
    $prd['sku']        = $_product->getSku();
    $prd['weight']     = $_product->getWeight();
    $prd['urlkey']     = $_product->getUrlKey();
    $prd['price']      = $_product->getPrice();
    $prd['metatitle']  = $_product->getMetaTitle();
    $prd['metakey']    = $_product->getMetaKeyword();
    $prd['metadesc']   = $_product->getMetaDescription();
    $prd['disc']       = $_product->getDescription();
    $prd['sdic']       = $_product->getShortDescription();

 $itsmArr[$m] = array($prd['id'],$prd['name'], $prd['sku'],$prd['urlkey'],$prd['price'],$prd['metatitle'],$prd['metakey'],$prd['metadesc'],$prd['disc'],$prd['sdic']); 
 echo $prd['id']."-".$prd['name']."</br>";
$m++;
}
array2csv($itsmArr,"");
?>

You can use what ever fields you want to get from the product collection. I hope this will help you or some one in future.

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.