Skip to content

Instantly share code, notes, and snippets.

@magemore
Created May 26, 2012 18:40
Show Gist options
  • Save magemore/2794888 to your computer and use it in GitHub Desktop.
Save magemore/2794888 to your computer and use it in GitHub Desktop.
Using Magento API to create categories
<?php
class Categories
{
private $client;
private $session;
private $tree;
private $categories;
public function __construct()
{
$this->client = new SoapClient('http://whitemediadev.com/shell-med/api/soap?wsdl');
$this->session = $this->client->login('login', 'password');
$this->tree = $this->client->call($this->session, 'catalog_category.tree');
}
public function getCategories()
{
if (!$this->categories) {
$this->categories = array();
foreach ($this->tree['children'][0]['children'] as $key => $value) {
$this->categories[$value['category_id']] = $value['name'];
}
}
return $this->categories;
}
public function addCategory($s)
{
$parentId = $this->tree['children'][0]['category_id'];
$this->client->call($this->session, 'catalog_category.create',
array(
$parentId,
array(
'name'=> $s,
'is_active'=>1,
'include_in_menu'=>1,
'available_sort_by'=>'position',
'default_sort_by'=>'position'
)
));
}
public function isExists($s)
{
foreach ($this->getCategories() as $key => $value) {
if ($s==$value) return true;
}
return false;
}
}
$categories = new Categories;
$importCategories = explode(PHP_EOL,file_get_contents('db/categories.csv'));
foreach ($importCategories as $key => $value) {
if ($categories->isExists($value)) {
echo $value.PHP_EOL;
}
else {
$categories->addCategory($value);
}
}
@susheelhbti
Copy link

db/categories.csv

can you attach here its sample

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment