4

I have the parent category "XXX" and having around 115 subcategories under the same.

I am trying to sort the subcategories listed under of specific parent category at the backend... enter image description here

4

Use like below.

protected $_categoryCollection;

public function __construct(
 \Magento\Backend\Block\Template\Context $context,
 \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollection,
  array $data = []
 )
{
   $this->_categoryCollection = $categoryCollection;
   parent::__construct($context, $data);
}
public function getCategoryCollection()
 {
   $categoryId = "your parent categoryId";
    $collection = $this->_categoryCollection->create()->load($categoryId)
    ->addAttributeToSelect('*')
    ->addAttributeToSort('position', 'asc');
    return $collection;
}
| improve this answer | |
  • in which file should I use.. or its a custom code? – 1990rk4 May 24 '19 at 6:20
  • @rohan.katkar, what is your exact requirement? you want to get all sub categories for parent category with sort? or to swap the admin categories programmatically? – jafar pinjar May 24 '19 at 6:21
  • to sort the all sub categories for spefic parent category... – 1990rk4 May 24 '19 at 6:22
  • @rohan.katkar, sort with what and where? you want retrieve the collection? – jafar pinjar May 24 '19 at 6:23
  • jafar, I am trying to sort subcategories of particular parent category.. my concern is just that where should I fix this... in core or as custom... looking at your code... it seems good.. but where to place it... getCategoryCollection() is defined in core... – 1990rk4 May 24 '19 at 6:26

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.