0

I get store categories from category helper, i need to order categories by entity id with my array. Here is my code

\Magento\Catalog\Helper\Category $categoryHelper,

...

$this->_categoryHelper = $categoryHelper;

public function getStoreCategories($sorted = true, $asCollection = true, $toLoad = true)
{
 
    $order = array(2,6,3,4,5);


    $menuitems = $this->_categoryHelper
                ->getStoreCategories($sorted , $asCollection, $toLoad) 
                ->addAttributeToFilter('entity_id', $order)
                ->addAttributeToFilter('include_in_menu', array('eq' => 1));
     
    return $menuitems;
}

Can somebody help me ?

Thanks

1 Answer 1

1

You can add the MySQL FIELD() expression to your collection:

$menuitems->getSelect()
    ->order(new \Zend_Db_Expr('FIELD(e.entity_id, ' . implode(',', $order).')'));

Your Answer

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

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