1

In Magento 2.2 I've written this little script.

    /**
     * Index constructor.
     *
     * @param Context               $context
     * @param StoreManagerInterface $storeManager
     * @param CategoryRepository    $categoryRepository
     * @param array                 $data
     */
    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager,
        CategoryRepository $categoryRepository,
        $data = []
    ) {
        $this->_storeManager       = $storeManager;
        $this->_categoryRepository = $categoryRepository;
        parent::__construct($context);
    }

    /**
     * @return ResponseInterface|Interceptor|ResultInterface|null
     * @throws NoSuchEntityException
     */
    public function execute()
    {
        $request = $this->getRequest();

        if ($request->isAjax() && $this->getRequest()->getParam('id') !== '0' ) {
            $parent_category_id = $this->getRequest()->getParam('id');
            $categoryObj        = $this->_categoryRepository->get($parent_category_id);
            $data               = $categoryObj->getChildrenCategories();

            /** @var Interceptor $resultJson */
            $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
            $resultJson->setData($data);

            return $resultJson;
        }

        return null;
    }

In the execute function the parent category id is send in the ajax request and works.

With that parent category id I ask the categoryRepository to get that category.

After that I ask the category object to get it's children categories.

This is send to the ResultFactory which should return a JSON string with the children categories.

In Magento 2.2 this works, in Magento 2.3 the ResultFactory returns an empty JSON string.

I've looked in the release notes, but I could not find anything about a refactor in the categoryRepository.

Frank Groot
  • 784
  • 2
  • 12
  • 37

1 Answers1

0

Solved it the following way, because it looks like getChilderenCategories is not available anymore in the CategoryRepository (I'm not sure).

    /**
     * Index constructor.
     *
     * @param Context               $context
     * @param StoreManagerInterface $storeManager
     * @param CategoryRepository    $categoryRepository
     * @param array                 $data
     */
    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager,
        CategoryRepository $categoryRepository,
        $data = []
    ) {
        $this->storeManager       = $storeManager;
        $this->categoryRepository = $categoryRepository;
        parent::__construct($context);
    }

    /**
     * @return ResponseInterface|Interceptor|ResultInterface|null
     * @throws NoSuchEntityException
     */
    public function execute()
    {
        $request = $this->getRequest();
        $data    = [];
        $parentCategoryId = $request->getParam('id');

        if ($request->isAjax() && $parentCategoryId !== '0') {
            $categoryObjects  = $this->categoryRepository->get($parentCategoryId);
            $childCategoryIds = explode(',', $categoryObjects->getChildren());

            if (array_filter($childCategoryIds)) {
                foreach ($childCategoryIds as $index => $id) {
                    /** @var \Magento\Catalog\Model\Category\Interceptor $category */
                    $category                 = $this->categoryRepository->get($id);
                    $data[$category->getId()] = $category->getData();
                }
            }

            /** @var Interceptor $resultJson */
            $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
            $resultJson->setData($data);

            return $resultJson;
        }

        return null;
    }
Frank Groot
  • 784
  • 2
  • 12
  • 37