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.
$this->resultFactory
in constructor$data
is not empty, the whole webshop configuration gets dumped when debugging...$this->resultFactory
gets inherited fromAbstractAction
class