0

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

enter image description here

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; 
}
2
  • Check if your other Environment has those category enabled properly Commented Sep 19, 2022 at 6:35
  • Yes these are enabled Commented Sep 19, 2022 at 7:03

1 Answer 1

0

The problem is with POSTMAN IDK why it is showing the same response over and over again, I call the API in Google Chorome and then call again on Postman issue resolved !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.