5

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';
    Mage::app();
    
    $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__)));
require_once(MAGENTOROOT.'/app/Mage.php');
umask(0); 
Mage::app('default');

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

$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!!

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

1 Answer 1

10

This should work:

<?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();
$products = Mage::getModel("catalog/product")->getCollection();
$products->addAttributeToSelect('category_ids');
$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), ",");
}
fclose($fp);

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

5
  • yes, it worked! thank you very much you!! :) Jul 19, 2013 at 13:36
  • @Marius If I want the categories names instead of categories ids?
    – Mukesh
    Jan 15, 2014 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, 2014 at 8:02
  • Varien_File_Csv
    – philwinkle
    May 7, 2014 at 15:18
  • What's an easy way to download every attribute using this method? @Marius
    – Head
    Feb 13, 2017 at 23:21

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