Skip to content

Instantly share code, notes, and snippets.

@andre-redstage
Last active January 24, 2018 17:29
Show Gist options
  • Save andre-redstage/74668c313206dd5d956e375fede0c658 to your computer and use it in GitHub Desktop.
Save andre-redstage/74668c313206dd5d956e375fede0c658 to your computer and use it in GitHub Desktop.
Get Magento categories from a M1 store and save to a M2 store using Magento webservices on M1 and M2.
<?php
class ImportCategories {
private $m1Client = null;
private $m1data = null;
private $m2data = null;
private $m1Session = null;
private $m2Categories = null;
public function __construct($m1data, $m2data) {
$this->m1data = $m1data;
$this->m2data = $m2data;
}
public function getM1Client() {
if (!$this->m1Client) {
$this->m1Client = new SoapClient($this->m1data['url']);
}
return $this->m1Client;
}
public function getM1Session() {
if (!$this->m1Session) {
$this->m1Session = $this->getM1Client()->login($this->m1data['username'], $this->m1data['password']);
}
return $this->m1Session;
}
public function getM1Categories() {
return $this->getM1Client()->call($this->getM1Session(), 'catalog_category.tree');
}
public function importCategories() {
$this->walkOnTree($this->getM1Categories());
}
public function walkOnTree($categoryTree, $parent_id = 0)
{
$current_item = $categoryTree;
if($current_item['category_id'] < 3)
{
$parent_id = $current_item['category_id'];
} else {
$category = $this->createCategory($current_item, $parent_id);
$parent_id = $category['category_id'];
}
$children = array_filter($current_item['children']);
if(!empty($children))
{
foreach ($children as $child) {
if($parent_id) {
$this->walkOnTree($child, $parent_id);
}
}
}
}
public function getSingleM1Category($category_id)
{
return $this->getM1Client()
->call(
$this->getM1Session(),
'catalog_category.info',
$category_id
);
}
public function getM2Category($name, $parent_id)
{
if(!$this->m2Categories) {
$ch = curl_init($this->m2data['url'] . "categories");
$curlOptions = array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
"Content-type: application/json", "Authorization: bearer " . $this->m2data['token']
)
);
curl_setopt_array( $ch, $curlOptions );
$this->m2Categories = json_decode(curl_exec($ch));
}
return $this->walkOnTreeM2($this->m2Categories, $name, $parent_id);
}
public function walkOnTreeM2($categoryTree, $name, $parent_id)
{
$current_item = $categoryTree;
if($current_item->name == $name
&& $current_item->parent_id == $parent_id)
{
return $current_item;
} else {
foreach ($current_item->children_data as $child) {
$m2item = $this->walkOnTreeM2($child, $name, $parent_id);
if($m2item) {
return $m2item;
}
}
}
return false;
}
public function createCategory($category_item, $parent_id)
{
$categoryData = $this->getSingleM1Category($category_item['category_id']);
$custom_attributes = [];
if (isset($categoryData['meta_title'])) {
$custom_attributes[] = [
'attribute_code' => 'meta_title',
'value' => $categoryData['meta_title']
];
}
if (isset($categoryData['meta_keywords'])) {
$custom_attributes[] = [
'attribute_code' => 'meta_keywords',
'value' => $categoryData['meta_keywords']
];
}
if (isset($categoryData['meta_description'])) {
$custom_attributes[] = [
'attribute_code' => 'meta_description',
'value' => $categoryData['meta_description']
];
}
$newCategoryData = [
'category' => [
'name' => $category_item['name'],
'level' => $category_item['level'],
'parent_id' => $parent_id,
'is_active' => $categoryData['is_active'],
'custom_attributes' => $custom_attributes
],
'saveOptions' => true
];
$category = $this->getM2Category($category_item['name'], $parent_id);
if ($category) {
$newCategoryData['category']['id'] = $category->id;
}
try {
$response = $this->createM2Category($newCategoryData);
if($response->id) {
echo $category_item['name'] . ' Created. Category ID: ' . $response->id . PHP_EOL;
} else {
echo 'Import failed' . PHP_EOL;
}
return [
'category_id' => $response->id,
'parent_id' => $response->parent_id
];
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
return false;
}
public function createM2Category($categoryData) {
$ch = curl_init($this->m2data['url'] . 'categories');
$curlOptions = array(
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($categoryData),
CURLOPT_HTTPHEADER => array(
"Content-type: application/json", "Authorization: bearer " . $this->m2data['token']
)
);
curl_setopt_array( $ch, $curlOptions );
return json_decode(curl_exec($ch));
}
}
$m1data = [
'url' => 'm1_v1_soap_url',
'username' => 'username',
'password' => 'pass'
];
$m2data = [
'url' => "M2_rest_url", //https://eroswhol.nextmp.net/rest/V1/
'token' => 'token'
];
$importCategories = new ImportCategories($m1data, $m2data);
$importCategories->importCategories();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment