3

I'm trying to exports products programatically with their category names. Here is my code to export it.

 <?php
require_once 'app/Mage.php';
umask(0);

// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);

$collection = Mage::getModel('catalog/product')
 ->getCollection()
 ->addAttributeToSelect('*'); 

foreach($collection as $product) {

  $_cat = array();
  $categoryName = array();

  foreach ($product->getCategoryIds() as $Id) {
    $_cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($Id);
  $categoryName = $_cat->getName();
  }
    fputcsv($output, array(
            $product->getSku(),
            $categoryName            
                     )
    );
}
?>

Products are associated to multiple categories. Though the code exports only one of the category names. How I can export all the category names associated to them programmatically?

1

The problem I see is that $product->getCategoryIds() returns an array, and you are using it later $Id to load the category. Also you can try this instead to load the categories for the store you want:

$rootId = Mage::app()->getStore($storeId)->getRootCategoryId();
$categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->addFieldToFilter('path', array('like'=> "1/$rootId/%"));

$categories->count();
1
  • Kindly check my completed code above and suggest where I need to make the modification. Thanks – amitshree Jul 27 '15 at 10:42
1

Are you sure that

$categoryName = $_cat->getName();

is not overriding the previous category name ?
Maybe try with array_push($categoryName, $_cat->getName).

1
  • Tried foreach ($product->getCategoryIds() as $Id) { $_cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($Id); $categoryName = array_push($categoryName, $_cat->getName); } fputcsv($output, array( $product->getSku(), $categoryName ) ); and it's exporting category id(only 1) to the csv. Please check the complete code above. – amitshree Jul 27 '15 at 10:25
1

Thanks for helping me out. implode() did the trick for me.

Here is the updated code:

<?php
require_once 'app/Mage.php';
umask(0);

// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);
$collection = Mage::getModel('catalog/product')
 ->getCollection()
 ->addAttributeToSelect('*') //Select everything from the table
 ->addUrlRewrite(); //Generate nice URLs
foreach($collection as $product) {
  $_cat = array();
  $categoryName = array();

  foreach ($product->getCategoryIds() as $Id) {
  $_cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($Id);
   $categoryName[] = $_cat->getName();
  }
  $comma_separated = implode(",", $categoryName);
    fputcsv($output, array(
           $comma_separated,

            str_replace('export.php/','',Mage::getBaseUrl()).$product->getUrlPath($category)
                     )
    );
}
?>
4
  • @ amitshree : our default data-flow profiles export return category id, how can i change to category name? – user51361 May 15 '17 at 8:50
  • @Rathinam To achieve that you need to overwrite core and do the modifications there. – amitshree May 15 '17 at 12:56
  • could u pls help me – user51361 May 17 '17 at 9:42
  • 1
    @Rathinam: I believe that requires a lot of customization. Maybe you can export category name separately using above method? – amitshree May 17 '17 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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