attiquer
5/17/2017 - 9:45 AM

Mage1 Export Products From Specific Category

Mage1 Export Products From Specific Category

<?php
// Create a file named catproexports.csv in the root

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();


$categoryIds = array(239); // here you can write category id you want to export products from it.


$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
$products->addAttributeToSelect('*');
$products->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left');
$products->addAttributeToFilter('category_id', array('in' => $categoryIds));



$fp = fopen('catproexports.csv', 'w');
$csvHeader = array("sku","name","category_ids","category_names");
fputcsv( $fp, $csvHeader,",");
foreach ($products as $product){
    $sku = $product->getSku();
    $proname = $product->getName();
    $catname = "";
    foreach ($product->getCategoryIds() as $id){
        $category = Mage::getModel('catalog/category')->load($id);

        if($catname != '')
        {
            $catname = $catname . "," . $category->getName();
        }
        else
        {
            $catname = $category->getName();
        }
    }

    $categoryIds = implode('|', $product->getCategoryIds());//change the category separator if needed
    fputcsv($fp, array($sku,$proname, $categoryIds,$catname), ",");
}
fclose($fp);
?>