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.

  • have you tried a step-by-step-debugger? is $data empty? – Philipp Sander Mar 12 at 9:29
  • I think you didn't initialize $this->resultFactory in constructor – Shoaib Munir Mar 12 at 9:30
  • $data is not empty, the whole webshop configuration gets dumped when debugging... – Frank Groot Mar 12 at 9:32
  • 2
    $this->resultFactory gets inherited from AbstractAction class – Frank Groot Mar 12 at 9:33
  • That's really confusing. +1 for your question – Shoaib Munir Mar 12 at 10:29
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;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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