can somebody suggest me a way or a script to export a csv file with all product SKUs & the categories those SKUs are associated to?

I've tried several things... but nothing seems to work. Magento's product export isn't working for me.. It gives me errors.

  1. I've tried the Aten's script [source code] but it prints the categories in json format..

  2. so i've also tried this:

    define('MAGENTO', realpath(dirname(__FILE__)));
    require_once MAGENTO . '/app/Mage.php';
    $category = Mage::getModel('catalog/category')->load($categoryId);
    $ids = $category->getCollection()->getAllIds();
    if ($ids) {
        $file = "var/import/catwithid.csv";
        file_put_contents($file,"category_ids, sku" . "\n");
        foreach ( $ids as $id ) {
          $string = $id . ', ' . $category->load($id) . "\n";
            file_put_contents($file, $string, FILE_APPEND);

and this

define('MAGENTOROOT', realpath(dirname(__FILE__)));

$products = Mage::getModel("catalog/product")->getCollection();
$products->addAttributeToFilter('status', 1);
$products->addAttributeToFilter('visibility', 4);

$fp = fopen('exports.csv', 'w');
$csvHeader = array("sku", "category_ids");
fputcsv( $fp, $csvHeader, $delimiter = ", ");

foreach( $products as $_product ) {
$sku = $_product->getData('sku');
$product = $_product->load($sku);
$product_row = array($sku);
fputcsv( $fp, $product_row, $delimiter = ", ");

I've also tried to merge them together but with no luck! :/

please help me!!

  • What sort of errors do you have with generic export? – user487772 Jul 19 '13 at 12:35

This should work:

error_reporting(E_ALL | E_STRICT);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
ini_set('display_errors', 1);
$products = Mage::getModel("catalog/product")->getCollection();
$products->addAttributeToFilter('status', 1);//optional for only enabled products
$products->addAttributeToFilter('visibility', 4);//optional for products only visible in catalog and search
$fp = fopen('exports.csv', 'w');
$csvHeader = array("sku", "category_ids");
fputcsv( $fp, $csvHeader,",");
foreach ($products as $product){
    $sku = $product->getSku();
    $categoryIds = implode('|', $product->getCategoryIds());//change the category separator if needed
    fputcsv($fp, array($sku, $categoryIds), ",");

put the script in a file on the same level as index.php and run it in the browser or command line.

|improve this answer|||||
  • yes, it worked! thank you very much you!! :) – Smith Jones Jul 19 '13 at 13:36
  • @Marius If I want the categories names instead of categories ids? – Mukesh Jan 15 '14 at 7:58
  • @Muk. Then you can load each category based on the id and get its name. foreach ($product->getCategoryIds() as $id){$category = Mage::getModel('catalog/category')->load($id); $name = $category->getName()}. You can even remember the loaded categories in an array to avoid loading the same category twice for performance. – Marius Jan 15 '14 at 8:02
  • Varien_File_Csv – philwinkle May 7 '14 at 15:18
  • What's an easy way to download every attribute using this method? @Marius – Supplement Feb 13 '17 at 23:21

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