Last active
May 10, 2016 17:02
-
-
Save vduglued/b99e19567b48fe18c1df3c98e7187f4b to your computer and use it in GitHub Desktop.
Export Magento 1.x categories in a format that is more readily usable with Magmi On-the-fly category creator/importer. More at https://aarpol.com/moving-multiple-magento-products-to-different-categories/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'abstract.php'; | |
class Codeproper_Shell_Export_Categories extends Mage_Shell_Abstract | |
{ | |
private $categoryNames = array(); | |
public function __construct() { | |
parent::__construct(); | |
set_time_limit(0); | |
} | |
public function run() | |
{ | |
$fh = fopen( | |
Mage::getBaseDir('base') . '/var/exported_categories.csv', | |
'w' | |
); | |
$this->putcsv($fh, array( | |
'sku', | |
'name', | |
'status', | |
'categories' | |
)); | |
$products = $this->getAllProducts(); | |
foreach ($products as $product) { | |
$row = array( | |
$product->getSku(), | |
$product->getName(), | |
($product->getStatus() == 1 ? 'Enabled' : 'Disabled'), | |
); | |
foreach ($this->getCategoryPaths($product) as $category) { | |
$row[] = $category; | |
} | |
$this->putcsv($fh, $row); | |
} | |
fclose($fh); | |
} | |
private function putcsv($fh, $rowData) | |
{ | |
foreach ($rowData as &$v) { | |
$v = '"' . str_replace('"', '""', $v) . '"'; | |
} | |
$data = implode(',', $rowData) . "\n"; | |
return fwrite($fh, $data); | |
} | |
private function getCategoryPaths($product) | |
{ | |
$paths = array(); | |
$categories = $product->getCategoryCollection() | |
->addAttributeToSelect('name'); | |
foreach ($categories as $category) { | |
$paths[] = $this->getCategoryPath($category); | |
} | |
return $paths; | |
} | |
private function getCategoryPath($category) | |
{ | |
$parentIds = $category->getParentIds(); | |
$pathItems = array(); | |
foreach ($parentIds as $parentId) { | |
$pathItems[] = $this->getCategoryName($parentId); | |
} | |
// add current category | |
$pathItems[] = $category->getName(); | |
return implode('/', $pathItems); | |
} | |
private function getCategoryName($categoryId) | |
{ | |
if (!array_key_exists($categoryId, $this->categoryNames)) { | |
$allCategories = Mage::getModel('catalog/category') | |
->getResourceCollection() | |
->addAttributeToSelect('name'); | |
foreach ($allCategories as $category) { | |
$this->categoryNames[$category->getId()] = $category->getName(); | |
} | |
if (!array_key_exists($categoryId, $this->categoryNames)) { | |
throw new Exception('Category name not found for category ' . $categoryId); | |
} | |
} | |
return $this->categoryNames[$categoryId]; | |
} | |
private function getAllProducts() | |
{ | |
return Mage::getModel('catalog/product') | |
->getCollection() | |
->addAttributeToSelect('name') | |
->addAttributeToSelect('sku') | |
->addAttributeToSelect('status'); | |
} | |
public function usageHelp() | |
{ | |
return <<<USAGE | |
Usage: php -f export_categories.php | |
USAGE; | |
} | |
} | |
$shell = new Codeproper_Shell_Export_Categories(); | |
$shell->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment