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

API Categories: Parameter to pass along
 
Roderik
Member
 
Avatar
Total Posts:  35
Joined:  2007-09-15
Belgium
 

I’m trying to convert a magento 0.6 site to the new 1.1 version. The normal update way doesn’t work but i’m getting somewhere with the REST module from the programming with magento ebook in combination with the webservices api.

I’m tying to recreate my category structure, but the wiki at http://www.magentocommerce.com/wiki/doc/webservices-api/api#category_api doesn’t have a description about what params to pass in the array.

I currently have:

$client = new SoapClient('http://magento.tenedos.local.vanderveer.be/api/?wsdl');
$session $client->login('test''testkey');
$storeId    Mage::app()->getStore()->getId();
$products Mage::getResourceModel('catalog/category_collection')->addAttributeToSelect('*');
$products->getEntity()->setStore($storeId);
$products->load();
foreach (
$products->toArray() as $product{
    
unset($product['entity_id']);
    unset(
$product['parent_id']);
    unset(
$product['store_id']);
    
var_dump($client->call($session'catalog_category.create', array(3$product$storeId)));
}

and it creates rows in the database, but nothing appears in the admin area, there is a difference in the row content between an original cat from the demo data and mine.

entity_id     entity_type_id     attribute_set_id     parent_id     created_at     updated_at     path     position     level
4      9      12      18      2008
-06-14 11:12:56      2008-06-14 11:12:56      1/3/18/4      4      3

entity_id     entity_type_id     attribute_set_id     parent_id     created_at     updated_at     path     position     level
151      9      0      0      2008
-04-22 22:11:23      2008-07-16 21:36:15      /151      151      1

In short, i’m just guessing my way along, and it ain’t going so smooth smile so i’ve got some questions

1. is there any documentation about the arrays that are being thrown around through the api? apart from the examples in the wiki?
2. the attribute_set_id that is missing in the database, the product example describes something like the code below. Am i right to guess this returns some sort of default value that i can use? If not, what does this mean?

$attributeSets $client->call($session'product_attribute_set.list');
$set current($attributeSets);
3. you will see the parent is also empty, though i pass along “3” as parent id, guess i don’t understand that as well
 
Magento Community Magento Community
Magento Community
Magento Community
 
JKwan13
Member
 
Total Posts:  63
Joined:  2008-06-12
 

1. Take a look at the newly updated category API : http://www.magentocommerce.com/wiki/doc/webservices-api/api/catalog_product

2.

$attributeSets $client->call($session'product_attribute_set.list');
$set current($attributeSets);
This code provides the first set of attibutes, and then $set[’set_id’] gives the ID number for that attribute set.  If you only have the Default attribute set, then this might be it, but I use some code to search for the “Default” attribute set.  Here it is:

// Find and Set attribute set id for "Default"
$attributeSets $client->call($session'product_attribute_set.list');
$default_id 0;
foreach (
$attributeSets as $set{
    
if($set['name'== "Default"{
        $default_id 
$set['set_id'];
        break;
    
}
}

Now $default_id will be the ID of the Default attribute set and you can use it in place of $set[’set_id’].

I’m having problems using the new category.create method though, take a look at this thread for the problem : http://www.magentocommerce.com/boards/viewthread/13146/

 
Magento Community Magento Community
Magento Community
Magento Community
 
Roderik
Member
 
Avatar
Total Posts:  35
Joined:  2007-09-15
Belgium
 

It seems you have indeed the same problem smile
I’ve got some more example code

$parentCategoryId 3;

        foreach (
$products->toArray() as $product{
            
if ($product["entity_id"3){
                var_dump
($product);
                
$newCategoryId $client->call($session,'category.create',
                    array(
$parentCategoryId,
                        array(
                            
'name'    =>    $product['name'],
                            
/*'attribute_set_id'    =>    $default_id,*/
                            
'is_active'    =>    1,
                            
'parent_id' =>     $parentCategoryId
                        
)
                    )
                );

                
var_dump($client->call($session'category.info', array($newCategoryId)));

                
$client->call($session'category.update', array($newCategoryId, array('parent_id'=>$parentCategoryId)));

                
var_dump($client->call($session'category.info', array($newCategoryId)));
                break;
            
}
        }

array
  
'entity_id' => string '5' (length=1)
  
'entity_type_id' => string '9' (length=1)
  
'attribute_set_id' => string '12' (length=2)
  
'parent_id' => string '3' (length=1)
  
'store_id' => string '0' (length=1)
  
'created_at' => string '2007-09-16 07:43:42' (length=19)
  
'updated_at' => string '2008-04-22 23:29:57' (length=19)
  
'is_active' => string '1' (length=1)
  
'meta_keywords' => string 'poker chips, 11.5g, ABS, metal insert, clay, 13.5g, suits, las vegas, laser sticker' (length=83)
  
'all_children' => string '5,28,29,30,36,37' (length=16)
  
'path_in_store' => string '5' (length=1)
  
'children' => string '28,29,30,36,37' (length=14)
  
'name' => string 'Chips' (length=5)
  
'display_mode' => string 'PRODUCTS' (length=8)
  
'landing_page' => string '0' (length=1)
  
'is_anchor' => string '1' (length=1)

array
  
'category_id' => string '404' (length=3)
  
'parent_id' => string '0' (length=1)
  
'is_active' => string '1' (length=1)
  
'position' => string '404' (length=3)
  
'level' => string '1' (length=1)
  
'created_at' => string '2008-07-27 11:35:25' (length=19)
  
'updated_at' => string '2008-07-27 11:35:25' (length=19)
  
'name' => string 'Chips' (length=5)
  
'url_key' => string 'chips' (length=5)
  
'path' => string '/404' (length=4)

array
  
'category_id' => string '404' (length=3)
  
'parent_id' => string '0' (length=1)
  
'is_active' => string '1' (length=1)
  
'position' => string '404' (length=3)
  
'level' => string '1' (length=1)
  
'created_at' => string '2008-07-27 11:35:25' (length=19)
  
'updated_at' => string '2008-07-27 11:35:26' (length=19)
  
'name' => string 'Chips' (length=5)
  
'url_key' => string 'chips' (length=5)
  
'path' => string '/404' (length=4)
 
Magento Community Magento Community
Magento Community
Magento Community
 
Roderik
Member
 
Avatar
Total Posts:  35
Joined:  2007-09-15
Belgium
 

Whoohoo ‘ive got the items in the tree!

There is some kind of checking code that compares the $parent_id to the path, or the other way around. This code inserts the cats from the old shop in the new one and they appear in the admin and the frond-end. Offourse all top level for now, needs some code to convert the old structure.

$parentCategoryId 3;

        foreach (
$products->toArray() as $product{
            
if ($product["entity_id"3){
                var_dump
($product);
                
$newCategoryId $client->call($session,'category.create',
                    array(
$parentCategoryId,
                        array(
                            
'name'    =>    $product['name'],
                            
/*'attribute_set_id'    =>    $default_id,*/
                            
'parent_id' =>     $parentCategoryId,
                            
'meta_keywords'    =>    (isset($product['meta_keywords'])?$product['meta_keywords']:null),
                            
'is_active'    =>    $product['is_active'],
                            
'is_anchor'    =>    $product['is_anchor'],
                            
'display_mode'    =>    $product['display_mode'],

                        )
                    )
                );

                
$client->call($session'category.update',array($newCategoryId, array(
                        
'parent_id'=>$parentCategoryId,
                        
'path'    => '1/'.$parentCategoryId.'/'.$newCategoryId
                    
)));

                
var_dump($client->call($session'category.info', array($newCategoryId)));

                
//var_dump($client->call($session, 'category.info', array($newCategoryId)));*/
                //break;
            
}
        }
 
Magento Community Magento Community
Magento Community
Magento Community
 
JKwan13
Member
 
Total Posts:  63
Joined:  2008-06-12
 

Thanks for code.  However, with your changes it updates the “parent_id” and “path”, unfortunately it still doesn’t show up in the frontend or admin.

This is the code I have:

$parentCategoryId 27;

// create new category
$newCategoryId $client->call($session,'category.create', array($parentCategoryId, array(
    
'name'        =>    'New Category Through Soap',
    
'is_active'    =>    1
)));
 
$client->call($session'category.update', array($newCategoryId, array(
    
'parent_id'    =>    $parentCategoryId,
    
'path'        => '1/'.$parentCategoryId.'/'.$newCategoryId,
    
'level' => 3
)));

$newCategoryData $client->call($session'category.info', array($newCategoryId));
var_dump($newCategoryData);

I also updated the “level”, to see if it would help.  Even though the “level” attribute was updated, it still doesnt show up on backend/frontend.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Roderik
Member
 
Avatar
Total Posts:  35
Joined:  2007-09-15
Belgium
 

This is the complete class that clears out the categories and imports all the categories from an old magento install.

http://gist.github.com/3024

I did one other change, but to the core magento class Mage_Catalog_Model_Api_Resource. I changed

protected $_ignoredAttributeCodes = array('entity_id''attribute_set_id''entity_type_id');
to
protected $_ignoredAttributeCodes = array();

because i need the same entity_id’s as in the original shop. Maybe the attribute_set_id and entity_type_id are needed to display in the frontend?

 
Magento Community Magento Community
Magento Community
Magento Community
 
JKwan13
Member
 
Total Posts:  63
Joined:  2008-06-12
 

Thanks Roderik for your help.  But getting into the actual code for Magento is getting way over my head.  I need time to learn Magento architecture, which I dont want to or have time to do right now.  I think I will just create categories manually and then use the api to enter the products.  I was able to figure out a way to search the category tree to find the category ids I’m looking for, so that is all I need.

 
Magento Community Magento Community
Magento Community
Magento Community
 
JKwan13
Member
 
Total Posts:  63
Joined:  2008-06-12
 

I finally found my problem.

I went to bug report #6056, and saw that allstarbooks said that I maybe needed to refresh the catalog rewrites or update the layered navigation indexes, so I tried that, but that didnt help.

I then thought it was the fact that maybe the database was just corrupted with the older data from previous installs and my new categories.  So I did a fresh install of both magento and the database.

After this, it started to work.  But as I played with it more, trying to create more categories and sub-categories, I figured out why it probably didnt work originally.  It was because I didn’t set up the path correctly.

I was trying to do something like:
1/$parentId/$newId

but I didnt think about the full path, it should of been like:
1/$grandparentid/$parentid/$newid

I can’t believe that I made such a dumb mistake, or rather that I didnt figure that out until I after I did a fresh install.

I still believe that this should be a bug though because we shouldn’t have to enter the path manually like this.  The $parentId argument in the original ‘category.create’ method should have been enough to work.

 
Magento Community Magento Community
Magento Community
Magento Community
 
JKwan13
Member
 
Total Posts:  63
Joined:  2008-06-12
 

An update, this was posted by the “Magento Team” in the bug report:

create() method was fixed in 23334 rev.
If you want to change parent_id, you should use move() method instead update(), because parent_id is not editable attribute.

Questions/Comments:
- How to do know how to check which revision you’re running?  I downloaded the latest 1.1.2 release from the svn alpha trunk, but I cant tell if this is a new release or not. (It still doesnt work in this revision)

- I tried using move() method and while it does work for me, its kind of backwards.  When you move a category under a parent, it will make the category to be to top of the list rather than the expected bottom of the list of sub-categories.  So I have to reverse my input array to input properly. 

Ex:
Parent Category
->Sub-Category 1

After Move:

Parent Category
->Sub-Category 2
->Sub-Category 1

(I guess I have to wait until the fixed version is released until I can properly enter categories)

UPDATE (Aug 15, 08): 1.13 still has the same problem

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