Magento 2 – export categories
Magento 2 has no export categories option in backend. It is strange, that no one has implemented this, but as developer we can do it by our own. I show you a simple script that generates a formatted output. You can adjust with a few lines of code to fits your needs.
Magento 2 – export categories
My current task: export categories from our new Magento 2 shop was not as easy as expected. I found no option for this in backend. The only solution was to create a simple script that does this. Ok, there is another possibility by SQL from the database, but a PHP script is easier, especially if you want filter category collection by attributes (for example to show only all active categories).
My full script, you can run it from Magento 2 root folder:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <?php use Magento\Framework\App\Bootstrap; require __DIR__ . '/app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); //get category factory $categoryCollectionFactory = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory'); $categoryCollection = $categoryCollectionFactory->create(); $categoryCollection->addAttributeToSelect('*'); $categoryArray = array(); foreach ($categoryCollection as $category) { $split = explode("/",$category->getPath()); $sorter = ""; foreach($split as $element) $sorter.= $element + 10000; $categoryArray[] = array($category->getPath(), $category->getLevel(), $category->getName(), $sorter); } usort($categoryArray, "usortTest"); $handle = fopen("category_export.txt", "w"); foreach($categoryArray as $category) { $path = $category[0]; $level = $category[1]; $name = $category[2]; $tree = ""; if($level > 0){ for($i = 0; $i < $level - 1; ++$i) $tree.= " "; } //echo $tree.$name."\n"; fwrite($handle,$tree.$name."\n"); } echo "found ".$categoryCollection->getSize()." categories\n"; fwrite($handle,"found ".$categoryCollection->getSize()." categories"); fclose($handle); function usortTest($a, $b) { return strcmp($a[3],$b[3]); } |
If you run this script, you will get a category_export.txt file with a formatted output of you category tree. You get all categories, there is no filter active. First we use a collection factory to get a category collection. In my first foreach loop I create an array with all needed category attributes. I also create a sort-able attribute out of category path, this is needed if you want to print out a formatted category tree. With usort I sort this array by the created attribute – see usortTest function for details.
The output is done in my second foreach loop. I create a file handle and print out one category per line. I use a tree variable to create a formatted tree structure. At the end I print out the count of all categories. That’s all.
Conclusion
Magento 2 has currently no export categories option. If you need an export for further development, you have to do it by your own. You can build an SQL Statement to do it directly from MySQL database or you write your own script. I showed you a simple script that fits my needs. You can use it and adjust it for your needs.
It’s not working. made export.php in pub folder error 500
You can see 500 error messages in your webserver logs. I bet you get an “Allowed memory size exhausted” error, which means your php process has not enough memory. The memory needed depends on how much categories you have. If more memory is not possible, you need to load your categories collection by pages of certain size.
Hi,
Export is done. How import the exported categories to another server?
yes i have same requirement any idea how we can that export categories to import in any other setup
+1
But How to same file i’ll import on other server ?