I am facing an issue on the Live site that category collection only returns the specific level categories the same code is working fine on Localhost
Here is My Code
public function getAllCategory(int $storeId = 0){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$baseUrl = $storeManager->getStore()->getBaseUrl();
$catagoryList=array();
$result=array();
$collection = $this->_categoryCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addAttributeToFilter('is_active', 1);
$collection->setStore($storeId);
if ($collection->count() < 1) {
$result[0]['success']=false;
$message = __('Category doesn\'t exist.');
$result[0]['message']=$this->appApiHelper->translate($message);
$result[0]['data']=null;
return $result;
}
$collectionChild = $this->_categoryCollectionFactory->create();
$collectionChild->addAttributeToSelect('*');
$collection->addAttributeToFilter('level', 4);
$collectionChild->addAttributeToFilter('is_active', 1);
$collectionChild->setStore($storeId);
$result[0]['success']=true;
if ($collectionChild->count() < 1) {
foreach ($collection as $category) {
$catagoryList=array();
$catagoryList['entity_id'] = $category['entity_id'];
$catagoryList['name'] = $category->getName();
if($category->getImageUrl() != null)
{
$catagoryList['image_url'] = $baseUrl.$category->getImageUrl();
}
else
{
$catagoryList['image_url'] = false;
}
$catagoryList['url_path'] = $category->getUrlPath();
$catagoryList['description'] = $category->getDescription();
$catagoryList['tag_line'] = $category->getTagLine();
$result[0]['data'][]=$catagoryList;
}
}else{
foreach ($collection as $category) {
$catagoryList=array();
$catagoryList['entity_id'] = $category['entity_id'];
$catagoryList['name'] = $category->getName();
if($category->getImageUrl() != null)
{
$catagoryList['image_url'] = $baseUrl.$category->getImageUrl();
}
else
{
$catagoryList['image_url'] = false;
}
$catagoryList['url_path'] = $category->getUrlPath();
$catagoryList['description'] = $category->getDescription();
$catagoryList['tag_line'] = $category->getTagLine();
foreach ($collectionChild as $childcategory) {
$childCatagoryList=array();
if ($childcategory['parent_id']==$category['entity_id']) {
$childCatagoryList['entity_id'] = $childcategory['entity_id'];
$childCatagoryList['name'] = $childcategory->getName();
$childCatagoryList['image_url'] = $childcategory->getImageUrl();
$childCatagoryList['url_path'] = $childcategory->getUrlPath();
$childCatagoryList['description'] = $category->getDescription();
$childCatagoryList['tag_line'] = $category->getTagLine();
$catagoryList['children'][] = $childCatagoryList;
}
}
$result[0]['data'][]=$catagoryList;
}
}
return $result;
}