Dear Magento Community,
We are building an multi-language store on Magento. For the configuration with a third party we are looking for a way to get the categories, by name/title, out of Magento into an export file. We succeeded doing this with the script below. Right now we are additionally searching for a way to get the names of the categories in the right language out of Magento.
Additionally we are searching for a way to get the script to do the right mark-up. So for example, we would love to have the parent category above the child category.
We have been looking for an answer for this for a long while now. Anyone has any spare thoughts for us, or is even capable to help us out with this?
<?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 = $category->getPath();
foreach($split as $element)
$sorter.= $element + 10000;
$categoryArray[] = array($category->getPath(), $category->getLevel(), $category->getName(), $category->getId(),$sorter);
}
usort($categoryArray, "usortTest");
$handle = fopen("category_export.txt", "w");
foreach($categoryArray as $category)
{
$path = $category[0];
$level = $category[1];
$name = $category[2];
$catid = $category[3];
$tree = "";
if($level > 0){
for($i = 0; $i < $level - 1; ++$i)
$tree.= " ";
}
//echo $tree.$name."\n";
fwrite($handle,$tree.$name." - ". $catid."\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]);
}
?>
Is it possible for you to use the API?
For example, using something like:
<?php $api_url = 'https://www.domain.com/rest/V1'; $userData = array("username" => "your_username", "password" => "your_password"); $curl = curl_init($api_url . "/integration/admin/token"); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($userData)); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData)))); $token = curl_exec($curl); $curl = curl_init($api_url . "/categories"); try { curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token))); $result = curl_exec($curl); $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); echo '<pre>'; var_dump($http_code); echo '<br /><br /><br />'; echo '--------------------------'; echo '<br /><br /><br />'; var_dump($result); echo '<br /><br /><br />'; echo '--------------------------'; echo '<br /><br /><br />'; $decode = json_decode($result); print_r($decode); echo '</pre>'; } catch (Exception $e) { dump($e); }
You'll get something like:
int(200) -------------------------- string(382) "{"id":2,"parent_id":1,"name":"Default Category","is_active":true,"position":1,"level":1,"product_count":0,"children_data":[{"id":3,"parent_id":2,"name":"My sample category","is_active":true,"position":1,"level":2,"product_count":0,"children_data":[{"id":4,"parent_id":3,"name":"My sample subcategory","is_active":true,"position":1,"level":3,"product_count":0,"children_data":[]}]}]}" -------------------------- stdClass Object ( [id] => 2 [parent_id] => 1 [name] => Default Category [is_active] => 1 [position] => 1 [level] => 1 [product_count] => 0 [children_data] => Array ( [0] => stdClass Object ( [id] => 3 [parent_id] => 2 [name] => My sample category [is_active] => 1 [position] => 1 [level] => 2 [product_count] => 0 [children_data] => Array ( [0] => stdClass Object ( [id] => 4 [parent_id] => 3 [name] => My sample subcategory [is_active] => 1 [position] => 1 [level] => 3 [product_count] => 0 [children_data] => Array ( ) ) ) ) ) )
There you can see: