magento2-docker/app/code/IpSupply/Prology/Helper/CategoryHelper.php

52 lines
1.4 KiB
PHP
Executable File

<?php
namespace IpSupply\Prology\Helper;
class CategoryHelper
{
protected $dir;
protected $objectManager;
public function __construct(
\Magento\Framework\Filesystem\DirectoryList $dir,
) {
$this->dir = $dir;
$this->objectManager = \Magento\Framework\App\ObjectManager::getInstance();
}
public function getAllCategories() {
$categories = $this->objectManager->Create('\Magento\Catalog\Model\ResourceModel\Category\Collection')
->addAttributeToSelect('*')
->addAttributeToSort('path', 'asc');
$data = [];
foreach($categories as $category){
$cat = array(
"id" => $category->getId(),
"parentId" => $category->getParentId(),
"name" => $category->getName()
);
$data[] = $cat;
}
$menu = [];
$this->createMenu($menu, $data, 2);
return array(
"data" => $data,
"menu" => $menu
);
}
function createMenu(&$result,$data, $parentId){
foreach ($data as $value) {
if ($value["parentId"] == $parentId) {
$value["child"] = array();
$child = &$value["child"];
array_push($result,$value);
$this->createMenu($child, $data, $value["id"]);
}
}
}
}