Magento Forum

Create Category Programmatically
 
zep007
Sr. Member
 
Avatar
Total Posts:  131
Joined:  2008-05-15
Connecticut
 

The website I am converting to Magento has about 100 categories, and 20,000 products.  The website also has an API module which I am using to do most of the importing.  I have my script set up to import products flawlessly, but I really need a way to create the categories based on the categories Im grabbing through the API

the product import script goes something like this:

require_once $dpath '/Mage.php';    //intialize Magento enviornment
Mage::app($si);           //declare which store

  
$newProduct Mage::getModel('catalog/product')
       
    ->
setStoreId($si)
    ->
setCategoryIds($cid)    //Works //apparel-->shirts
    
->setAttributeSetId($asid)  //Works//shirts attributes
    
    
->setSku('4you6')        //THIS MUST BE UNIQUE!! 
    
->setPrice(15.49)        //Works   
    
->setName('4You T Shirt 4')  //Works
    
->setManufacturer(20)    //Works
    
->setStatus(1)          //Works - 1 assumes enabled
    
->setWeight(5)          //Works 
    
->setCost(13.50)        //Works 
    
->setMetaDescription('Descript'//Works
    
->setShortDescription('short-desc'//Works
    
->setDescription('Desc')            //Works
    
->setModel('4you4')                 //Works
    
->setMetaTitle('test')              //Works  
    
->save(); 
    
  echo 
'New Product ID: '.$newProduct->getId();

Now all I need is a snippet that can be used to create the category.  I assume its floating out there because the oscommerce import mod exists, but for all my searching - I cant find it.

Thanks for any help!

 
Magento Community Magento Community
Magento Community
Magento Community
 
zep007
Sr. Member
 
Avatar
Total Posts:  131
Joined:  2008-05-15
Connecticut
 

Anyone got this one?

{BUMP}

 
Magento Community Magento Community
Magento Community
Magento Community
 
jeff.d
Sr. Member
 
Total Posts:  103
Joined:  2008-05-26
 

I’m not sure if that method will work anymore. I used this method to create my categories on version 1.1.3. Don’t forget to setup the web api user name and password:

<?php
 
$proxy 
= new SoapClient('http://www.yourserver.com/api/soap/?wsdl');
$sessionId $proxy->login('userid''password');
$allCategories $proxy->call($sessionId'category.tree'); // Get all categories.
 
 //first you connect and select the db
$link mysql_connect('dbserver''dbname''dbpassword')
or die(
"Could not connect");
mysql_select_db('dbname'$link) or die("Could not select magento");

//then perform your query
$result mysql_query('SELECT * FROM web_category ORDER BY cat_desc'$link) or die("Query failed");
//now you've got a result set you need to got thru each result.
while ($row mysql_fetch_assoc($result))
{
// echo $row["cat_desc"]."<br>";
try {
    
// create new category
    
    
$newCategoryId $proxy->call($sessionId'category.create'
        array(
3,array('name'=>$row["cat_desc"]'is_active'=>"1"'description'=>$row["cat_desc"])));
    
    echo 
$row["cat_desc"]."<br>";
}
catch (Exception $e){
    
echo $e->getMessage();
}


//finally you should free up all the resources used.
mysql_free_result($result);
mysql_close($link);
?>

Hope this helps

 
Magento Community Magento Community
Magento Community
Magento Community
 
zep007
Sr. Member
 
Avatar
Total Posts:  131
Joined:  2008-05-15
Connecticut
 

Thank you for your reply!  I have been thinking about upgrading, and your indication that this may not work with 1.1.4 sits heavily, I suppose the best approach will be to scrap these files and start over with the built in api. rolleyes

EDIT: does the api work in 1.1.4 ? It seems this script silently fails right after

$proxy = new SoapClient('http://www.yourserver.com/api/soap/?wsdl');

strange, as I have other scripts that can grab info from other external APIs without a hitch.  In my PHPinfo I see both server and client are active as well, any suggestions?  Should this be an issue I start another post for?

EDIT: I started another post
http://www.magentocommerce.com/boards/viewthread/17021/

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