Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Export & import categories
 
marioseq
Jr. Member
 
Total Posts:  2
Joined:  2010-04-07
 
pdxwebhead - 13 February 2009 09:01 PM

Hey All,

I have been struggling with this issue of exporting categories for a few days. I am partially through solving it but, I have hit my limit in terms of programming skills.

I have managed to get the category tree out of my database using the SOAP Web Services interface using the following method.

**And I am fully aware that I may be doing this all backwards—that being said this is what I have managed to do..

First: I created a web services user in Admin -> Web Services -> Add new user
Second: I created a new Role for that user in Admin-> Web Servivces -> Roles and gave it full access to all services.
Third: I assigned that new role to the new user
My Webservices user is “soaper” and the key or password is also “soaper”

Next I created a script by cobbling together some code fragments I found in the API services page and some other code floating around online.

It looks like this:

<?php

    $proxy 
= new SoapClient('http://127.0.0.1:8888/magento/index.php/api/?wsdl');
    
$sessionId $proxy->login('soaper''soaper');
    
$result $allCategories $proxy->call($sessionId'category.tree');
    
    echo 
'<pre>';
    
print_r($result);
    echo 
'<pre>';
?>

I saved this code as a PHP file called “get_categories.php” and dropped it in the root folder for my Magento installation.  Then to call it, I simply pointed my browser to the file—something like this: http://yourmagentohost/get_categories.php

And boom, it spits out what looks like the xml schema for the category tree with all of your categories and related goodness.

Array
(
[category_id] => 1
[parent_id] => 0
[name] => Root Catalog
[position] => 2
[level] => 0
[children] => Array
(
[0] => Array
(
[category_id] => 2
[parent_id] => 1
[name] => Default Category
[is_active] => 1
[position] => 2
[level] => 1
[children] => Array
(
[0] => Array
(
[category_id] => 6
[parent_id] => 2
[name] => Mens
[is_active] => 1
[position] => 3
[level] => 2
[children] => Array
(
[0] => Array
(
[category_id] => 7
[parent_id] => 6
[name] => Outerwear
[is_active] => 1
[position] => 2
[level] => 3
[children] => Array
(

My question is this; can anyone help extend this code so it’s flattened and can be read by Excel?

Hello,
When creating a user unders web services
it ask for an
“api key” and “api key confirmation”

where do i get this info

 
Magento Community Magento Community
Magento Community
Magento Community
 
armababy
Jr. Member
 
Total Posts:  10
Joined:  2009-11-21
 

Great script you got there.

So only problem after importing categories and you go to Catalog>Manage Categories they are not added there.
If you check in database table dataflow_batch_import has all imported data in it, but no store is populated with actual categories. (look like this a:2:{s:5:"store”;s:7:"default”;s:10:"categories”;s:29:"MyCategory";}}

Anyone can help to solve this please?

I’m running Magento ver. 1.4.0.1

 
Magento Community Magento Community
Magento Community
Magento Community
 
akbarihardik
Jr. Member
 
Total Posts:  15
Joined:  2009-09-08
 

Guys,

Simple and short script i found at http://www.sonassi.com/knowledge-base/quick-script-to-export-categories-with-ids/comment-page-1/#comment-26862

 
Magento Community Magento Community
Magento Community
Magento Community
 
blitux
Jr. Member
 
Total Posts:  8
Joined:  2009-12-15
 
santhosh kumar - 28 February 2009 01:56 AM
1. add the model to over ride the local folder.
<models>
            <
catalog>
                <
rewrite>
                    <
convert_adapter_category>Mycomp_Catalog_Model_Convert_Adapter_Category</convert_adapter_category>
                </
rewrite>
            </
catalog>
        </
models>

2. Put this code in the file in app/code/local/mycomp/Catelog/Model/Convert/Adapter/Category.php

class Mycomp_Catalog_Model_Convert_Adapter_Category
    
extends Mage_Eav_Model_Convert_Adapter_Entity
{
    
protected $_categoryCache = array();

    protected 
$_stores;

    
/**
     * Category display modes
     */
    
protected $_displayModes = array( 'PRODUCTS''PAGE''PRODUCTS_AND_PAGE');

    public function 
parse()
    
{
        $batchModel 
Mage::getSingleton('dataflow/batch');
        
/* @var $batchModel Mage_Dataflow_Model_Batch */

        
$batchImportModel $batchModel->getBatchImportModel();
        
$importIds $batchImportModel->getIdCollection();

        foreach (
$importIds as $importId{
            
//print '<pre>'.memory_get_usage().'</pre>';
            
$batchImportModel->load($importId);
            
$importData $batchImportModel->getBatchData();

            
$this->saveRow($importData);
        
}
    }

    
/**
     * Save category (import)
     *
     * @param array $importData
     * @throws Mage_Core_Exception
     * @return bool
     */
    
public function saveRow(array $importData)
    
{
        
if (empty($importData['store'])) {
            
if (!is_null($this->getBatchParams('store'))) {
                $store 
$this->getStoreById($this->getBatchParams('store'));
            
else {
                $message 
Mage::helper('catalog')->__('Skip import row, required field "%s" not defined''store');
                
Mage::throwException($message);
            
}
        } 
else {
            $store 
$this->getStoreByCode($importData['store']);
        
}

        
if ($store === false{
            $message 
Mage::helper('catalog')->__('Skip import row, store "%s" field not exists'$importData['store']);
            
Mage::throwException($message);
        
}

        $rootId 
$store->getRootCategoryId();
        if (!
$rootId{
            
return array();
        
}
        $rootPath 
'1/'.$rootId;
        if (empty(
$this->_categoryCache[$store->getId()])) {
            $collection 
Mage::getModel('catalog/category')->getCollection()
                ->
setStore($store)
                ->
addAttributeToSelect('name');
            
$collection->getSelect()->where("path like '".$rootPath."/%'");

            foreach (
$collection as $cat{
                $pathArr 
explode('/'$cat->getPath());
                
$namePath '';
                for (
$i=2$l=sizeof($pathArr); $i<$l$i++) {
                    $name 
$collection->getItemById($pathArr[$i])->getName();
                    
$namePath .= (empty($namePath) ? '' '/').trim($name);
                
}
                $cat
->setNamePath($namePath);
            
}

            $cache 
= array();
            foreach (
$collection as $cat{
                $cache[strtolower
($cat->getNamePath())$cat;
                
$cat->unsNamePath();
            
}
            $this
->_categoryCache[$store->getId()$cache;
        
}
        $cache 
=& $this->_categoryCache[$store->getId()];

        
$importData['categories'preg_replace('#\s*/\s*#''/'trim($importData['categories']));
        if (!empty(
$cache[$importData['categories']])) {
            
return true;
        
}

        $path 
$rootPath;
        
$namePath '';

        
$i 1;
        
$categories explode('/'$importData['categories']);
        foreach (
$categories as $catName{
            $namePath 
.= (empty($namePath) ? '' '/').strtolower($catName);
            if (empty(
$cache[$namePath])) {

                $dispMode 
$this->_displayModes[2];

                
$cat Mage::getModel('catalog/category')
                    ->
setStoreId($store->getId())
                    ->
setPath($path)
                    ->
setName($catName)
                    ->
setIsActive(1)
                    ->
setIsAnchor(1)
                    ->
setDisplayMode($dispMode)
                    ->
save();
                
$cache[$namePath] $cat;
            
}
            $catId 
$cache[$namePath]->getId();
            
$path .= '/'.$catId;
            
$i++;
        
}

        
return true;
    
}

    
/**
     * Retrieve store object by code
     *
     * @param string $store
     * @return Mage_Core_Model_Store
     */
    
public function getStoreByCode($store)
    
{
        $this
->_initStores();
        if (isset(
$this->_stores[$store])) {
            
return $this->_stores[$store];
        
}
        
return false;
    
}

    
/**
     *  Init stores
     *
     *  @param    none
     *  @return      void
     */
    
protected function _initStores ()
    
{
        
if (is_null($this->_stores)) {
            $this
->_stores Mage::app()->getStores(truetrue);
            foreach (
$this->_stores as $code => $store{
                $this
->_storesIdCode[$store->getId()$code;
            
}
        }
    }
}

?>

Hi! First of all, thanks for the code. It works very well.

I have a question, Is there a way to modify the code to import category_id exported from another script?

For example:

“store”,"categories”,"category_id"
“default”,"category1”,"156"
“default”,"category1/subcat1”,"186"
...

This way, I can export products with only category_id, comma separated, and then import all products and categories.

Thanks in advance.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Charles @ MJM
Jr. Member
 
Avatar
Total Posts:  21
Joined:  2008-03-19
 

To all experiencing \"Declared adapter catalog/convert_adapter_category\” and similar errors:

Disable your cache when importing and it will fix it!

I am now stuck on the \"Processed 0% 0/160 records\” (just using the default csv file from the example to test)… gonna walk away from my computer for a bit and come back in hopes that it processed the records.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Lukasz 'Severiaan' Grela
Jr. Member
 
Total Posts:  19
Joined:  2010-04-05
Southampton
 

in CSV file, first row should be “store”, “categories” as opposed to “Store”, “categories”:) I am still fighting with that:) but at least I don’t have this error

 
Magento Community Magento Community
Magento Community
Magento Community
 
Grut
Sr. Member
 
Total Posts:  164
Joined:  2009-12-03
Ukraine
 

Hi there,

Did I mentioned about convinient “Import/Export Wizard” of Store Manager for Magento in this thread? wink

 
Magento Community Magento Community
Magento Community
Magento Community
 
Abdulkawi
Jr. Member
 
Total Posts:  15
Joined:  2010-04-08
 

Hi! First of all, thanks for the code. It works very well.

I have a question, Is there a way to modify the code to import category_id exported from another script?

For example:

“store”,"categories”,"category_id"
“default”,"category1”,"156"
“default”,"category1/subcat1”,"186"
...

This way, I can export products with only category_id, comma separated, and then import all products and categories.

Thanks in advance.

I also have the same question. Anyone able to help with this? I would like it so that I can create the category id myself alongside the category name.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Charles @ MJM
Jr. Member
 
Avatar
Total Posts:  21
Joined:  2008-03-19
 
Grut - 15 June 2010 05:52 AM

Hi there,

Did I mentioned about convinient “Import/Export Wizard” of Store Manager for Magento in this thread? wink

I would give it a shot if the trial let me do more than just 1 product.. that’s not enough to test my needs.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Grut
Sr. Member
 
Total Posts:  164
Joined:  2009-12-03
Ukraine
 
Charles @ MJM - 16 June 2010 03:07 PM

Grut - 15 June 2010 05:52 AM
Hi there,

Did I mentioned about convinient “Import/Export Wizard” of Store Manager for Magento in this thread? wink

I would give it a shot if the trial let me do more than just 1 product.. that’s not enough to test my needs.

I sent you PM. Admit that demo version has limited functionality, at the same time trial version is full but limited by time.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Abdulkawi
Jr. Member
 
Total Posts:  15
Joined:  2010-04-08
 

Anyone able to help me? I need to be able to use Hucke-Media method to create the category id myself in the csv. =[

 
Magento Community Magento Community
Magento Community
Magento Community
 
remie
Jr. Member
 
Total Posts:  17
Joined:  2009-02-13
 
Abdulkawi - 18 June 2010 12:28 PM

Anyone able to help me? I need to be able to use Hucke-Media method to create the category id myself in the csv. =[

I have the same exact need.  Trying to match up category ID’s on the category CSV to a product CSV with 1000 items already labeled with category ID numbers.

Any word on adding category ID’s to the CSV?

I’m gonna try figuring it out.  If I get anywhere I’ll post.

 
Magento Community Magento Community
Magento Community
Magento Community
 
olekhy
Jr. Member
 
Total Posts:  2
Joined:  2010-07-01
 

I’ve extended given code for 1.4.1.0

$categories explode('/'$importData['categories']);
        foreach (
$categories as $catName{
            $namePath 
.= (empty($namePath) ? '' '/').strtolower($catName);
            if (empty(
$cache[$namePath])) {

                $dispMode 
$this->_displayModes[2];

                
$cat Mage::getModel('catalog/category')

                    ->
setStoreId($store->getId())
                    ->
setPath($path)
                    ->
setName($catName)
                    
/* see for attrib names and values db table eav_attribute  */
                    
->setIsActive(1)
                    ->
setIsAnchor(1)
                    ->
setIncludeInMenu(1)
                    ->
setDisplayMode($dispMode)
                    ->
save();

                
$cache[$namePath] $cat;
            
}
            $catId 
$cache[$namePath]->getId();
            
$path .= '/'.$catId;
            
$i++;
 
Magento Community Magento Community
Magento Community
Magento Community
 
Abdulkawi
Jr. Member
 
Total Posts:  15
Joined:  2010-04-08
 
remie - 06 July 2010 08:20 PM

Abdulkawi - 18 June 2010 12:28 PM
Anyone able to help me? I need to be able to use Hucke-Media method to create the category id myself in the csv. =[

I have the same exact need.  Trying to match up category ID’s on the category CSV to a product CSV with 1000 items already labeled with category ID numbers.

Any word on adding category ID’s to the CSV?

I’m gonna try figuring it out.  If I get anywhere I’ll post.

Let me know how it goes, looking forward to this!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Watercooling
Jr. Member
 
Total Posts:  28
Joined:  2008-07-24
 

Hi olekhy,

is setIncludeInMenu(1) working for you?
I tried it but its ignored during import.
Furthermore i cant find any Block using setIncludeInMenu

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top