0

I have code for export like sku, category id and category path name of sku,

code :

<?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('var/export/exports.csv', 'w');
$csvHeader = array("sku", "category_ids","category_name");
fputcsv($fp, $csvHeader,",");
$cat_name = array();
$cat_array = array();
$categoryFullPaths = getCategoryData();
foreach ($products as $product) {
    $sku = $product->getSku();
    $categoryIds = implode('/', $product->getCategoryIds()); //change the category separator
    $_cate_name = join(',', get_values_for_keys($categoryFullPaths, $product->getCategoryIds()));
    fputcsv($fp, array($sku, $categoryIds,$_cate_name), ",");
}
fclose($fp);

function get_values_for_keys($mapping, $keys)
{
    $output_arr = array();
    foreach ($keys as $key) {
        $output_arr[] = $mapping[$key];
    }
    return $output_arr;
}

function getCategoryData()
{
    $category = Mage::getModel('catalog/category');
    $tree = $category->getTreeModel();
    $tree->load();

    $ids = $tree->getCollection()->getAllIds();
    $categories = array();
    $rootCategoryId = Mage::app()->getStore('english')->getRootCategoryId();
    if ($ids) {
        foreach ($ids as $id) {
            $category->load($id);
            $categories[$id]['name'] = $category->getName();
            $categories[$id]['path'] = $category->getPath();
        }
        foreach ($ids as $id) {
            $path = explode('/', $categories[$id]['path']);
            $string = '';
            foreach ($path as $pathId) {
                if ($pathId == 1 || $rootCategoryId == $pathId) continue;  // Uncomment commented code if you want to remove Default Category name.
                $string.= $categories[$pathId]['name'] . '/';
            }

            $categoryFullPaths[$id] = rtrim($string,"/");
        }
    }
    return $categoryFullPaths;
}

Its perfectly working in localhost, when I try to live site its not working

  • Can you tell me, which error show when you try on server? – Dhiren Vasoya Jul 22 '17 at 10:20
  • Actually not error, the field only not export, in my localhost its export like Sku, Category_id, category path name, It's working good, when I try to run this code on my live site the SKU column, not export and category_id and category_path_name only export. – user51361 Jul 22 '17 at 10:27
  • and also not exactly export my all products category_id and category_path_name, it's only minimum count of category_id and category_path_name only export – user51361 Jul 22 '17 at 10:29
  • Can you put your code in try catch block, So it generate proper error. So we are able to solve the issue. – Dhiren Vasoya Jul 22 '17 at 10:29
  • Can you put your code in try catch block : how can i do it? – user51361 Jul 22 '17 at 10:30
0

You need to do your code like this,

<?php
try
{
    error_reporting(E_ALL | E_STRICT);
    define('MAGENTO_ROOT', getcwd());
    $mageFilename = MAGENTO_ROOT . '/app/Mage.php';
    require_once $mageFilename;
    Mage::app();

    function getCategoryData()
    {
        $category = Mage::getModel('catalog/category');
        $tree = $category->getTreeModel();
        $tree->load();

        $ids = $tree->getCollection()->getAllIds();
        $categories = array();
        $rootCategoryId = Mage::app()->getStore('english')->getRootCategoryId();
        if ($ids)
        {
            foreach ($ids as $id)
            {
              $category->load($id);
              $categories[$id]['name'] = $category->getName();
              $categories[$id]['path'] = $category->getPath();
            }
            foreach ($ids as $id)
            {
                $path = explode('/', $categories[$id]['path']);
                $string = '';
                foreach ($path as $pathId)
                {
                      if($pathId == 1 || $rootCategoryId == $pathId ) continue; 
                      $string.= $categories[$pathId]['name'] . '/';
                }
                $categoryFullPaths[$id] = rtrim($string,"/");
            }
        }
        return $categoryFullPaths;
    }

    function get_values_for_keys($mapping, $keys) 
    {
        $output_arr = array();
        foreach($keys as $key) 
        {
            $output_arr[] = $mapping[$key];
        }
        return $output_arr;
    }

    $products = Mage::getModel("catalog/product")->getCollection();
    $products->addAttributeToSelect('category_ids');

    $fp = fopen('var/export/exports.csv', 'w');
    $csvHeader = array("sku", "category_ids","category_name");
    fputcsv( $fp, $csvHeader,",");
    $cat_name = array();
    $cat_array = array();
    $categoryFullPaths = getCategoryData();
    foreach ($products as $product)
    {
        $sku = $product->getSku();
        $categoryIds = implode('/', $product->getCategoryIds());//change the category separator
        $_cate_name = join(',', get_values_for_keys($categoryFullPaths, $product->getCategoryIds()));
        fputcsv($fp, array($sku, $categoryIds,$_cate_name), ",");
    }
    fclose($fp);
}
catch (Exception $e) 
{
    echo $e->getMessage();
}
?>
  • nope, everything export except sku, the sku column empty. can i get help? – Gem Sep 26 '18 at 5:54
  • yes, you can get help. the better way is you need to asp seprate question for this. So you get answer from other as well. – Dhiren Vasoya Sep 26 '18 at 9:13
  • stackoverflow.com/q/52514890/5906850 @Dhiren – Gem Sep 26 '18 at 9:53

Your Answer

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