2

How can I export products by category name instead of category id?

enter image description here

1

2 Answers 2

0
<?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_name');
$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_name");
fputcsv( $fp, $csvHeader,",");
foreach ($products as $product){
    $sku = $product->getSku();
    $categoryIds = implode('|', $product->getCategoryIds());//change the category separator if needed
    $_category = Mage::getModel('catalog/category')->load($categoryIds);
    $categoryData = $_category->getName();
    fputcsv($fp, array($sku, $categoryData), ",");
}
fclose($fp);

Put these code in root file and then run in browser it will create a csv file with category name

enter image description here

8
  • let me check...
    – Gem
    Aug 4, 2016 at 12:09
  • If i export product by Dataflow i get product category like id, so i need to get by category name, because of i need to import.
    – Gem
    Aug 4, 2016 at 12:12
  • there only i get by category name, because its easily to import
    – Gem
    Aug 4, 2016 at 12:13
  • 1
    bro, i want to get category name at dataflow export file,
    – Gem
    Aug 4, 2016 at 12:25
  • 1
    then only i can import by dataflow import
    – Gem
    Aug 4, 2016 at 12:25
0

I have build appropriate logic in my existing project, Need to some ractify in code.

  public function _getCsvHeaders()
{
     $_columns = array(
         "type_id",
         "sku",
         "category_names", 
         "name",
         "url",
         "desc",
         "price",
         "images",
         "department",
         "xr_product_images",
         "description",
         "short_description",
    );
    return $_columns;
}

AND

public function exportproductbyCategoryName($categoryName = null , $limit = 100)
{
    try
    {
        $products = Mage::getModel('catalog/product')
                            ->getCollection()
                            ->addAttributeToFilter('price', array('gt' => 0))
                            ->setPageSize($limit)
                            ->setCurPage(1)
                            ->setOrder('entity_id', 'DESC')
                           ;
        $io = new Varien_Io_File();
        $path = Mage::getBaseDir('var') . DS . 'producttypeexport' . DS;
        $name = 'product-bussinesstype-'.$bussinessType.'.csv';
        $file = $path .   $name;
        $io->setAllowCreateFolders(true);
        $io->open(array('path' => $path));
        $io->streamOpen($file, 'w+');
        $io->streamLock(true);
        $io->streamWriteCsv($this->_getCsvHeaders());
        $headerData = array();
        $importData = 0;
        $columns = $this->_getCsvHeaders();
        foreach($products as $product)
        {
          $Prduct = mage::getModel('catalog/product')->load($product->getEntityid());
          $prod = Mage::helper('catalog/product')->getProduct($Prduct->getEntityId(), null, null);
          $attributes = $prod->getTypeInstance(true)->getSetAttributes($prod);
          $galleryData = $prod->getData('media_gallery');
          $categoryIds = $Prduct->getCategoryIds();
          $names = array();
          foreach($categoryIds as $categoryId)
          {
             $category = Mage::getModel('catalog/category')->load($categoryId);
             $names[] = $category->getName(); 
          }
          $categorynames = json_encode($names);                
          $CsvData = array();
          foreach($columns as $column)
          {
             if($column == 'category_names')
             {
                $CsvData[] = $categorynames;
                continue;  
             }
             $CsvData[] = $Prduct->getData($column);
          }
          $io->streamWriteCsv($CsvData);
          $importData++;
          continue;
        }
        return $file;
    }
    catch(Exceptin $e)
    {
        return $e->getMessage();;
    }
}

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.