3

I want to export my products in CSV file. I have used DataFlow profiles Export Products functionality but it exports products with category_ids e.g. 2,4,56

I want to export category names with category ids

My Category with ID 2 is ABC and the Path for this category is Root Catalog > Parent Category > ABC

I need to export path of the category like Parent Category/ABC instead of 2 and so on for other categories.

The final export data will look something like below.

SKU       Category ID   Category Path
-----------------------------------------------------------------------------
sku1        2,4,56      Men/Tees, Knits and Polos, Accessories/Bags & Luggage
sku2        6,8,12      Sale, Sale/Men/Shorts, Sale/Home & Decor
sku3        14,22       Men/New Arrivals, Sale/Accessories

I have 17k+ products on my store.

6
  • get them from where?
    – Marius
    May 25, 2017 at 6:04
  • Dataflow profiles export
    – Gem
    May 25, 2017 at 6:05
  • snag.gy/Jywe8C.jpg
    – user51361
    May 25, 2017 at 6:10
  • @Marius : Actually direct export from magento not working because of my products count, Dataflow profiles export return category id only, i need category name.
    – user51361
    May 25, 2017 at 6:15
  • 1
    maybe you should explain your problem in the question not the comments.
    – Marius
    May 25, 2017 at 6:16

1 Answer 1

3
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","category_name");
    fputcsv( $fp, $csvHeader,",");
    $cat_name = array();
    $cat_array = array();
    $cat = Mage::getModel('catalog/category');
    foreach ($products as $product)
    {
        $sku = $product->getSku();
        $categoryIds = implode('|', $product->getCategoryIds());//change the category separator if needed
        $cat_name = array();
        $cat_array = $product->getCategoryIds();
        for($k = 0; $k < count($cat_array); $k++)
        {   
            $id =  $cat_array[$k];
            $cat->load($id);
            $cat_name[] =  $cat->getName();
        }
        $_cate_name = implode('|',$cat_name);
        fputcsv($fp, array($sku, $categoryIds,$_cate_name), ",");
    }
fclose($fp);

Make file on root and use above code, hope this will work :)

6
  • its working perfectly.
    – user51361
    May 25, 2017 at 8:59
  • 1
    How can i used this code for default magento export code part, so i get category name when i export dataflow profiles.
    – user51361
    May 25, 2017 at 9:00
  • 2
    For that you have to override magento's default functionality
    – user00247
    May 25, 2017 at 9:05
  • Now sub categories path only included, may i know how can i include main category path also......eg: sku : 112 category_name : electronics/mobiles/brands. Now exported csv look like sku : 112 category_name : mobiles/brands,, How can i get my category_name like eg: sku : 112 category_name : electronics/mobiles/brands
    – user51361
    May 25, 2017 at 9:31
  • is it possible, magento direct export return like what i told,,,
    – user51361
    May 25, 2017 at 9:45