Alternative to Magento 2 Deprecated getCollection() method.

Many of us have been experienced that the use of the getCollection() method has been deprecated. The getCollection() method is available in your Model class. You might have faced that many times while calling product, quote, or order collection. Therefore, If you are using PhpStorm it’s showing a deprecated method like this screenshot.

Alternative to Magento 2 Deprecated getCollection() method

There are two alternatives. We will see both alternatives by example. However, Magento 2 always recommends using the Search Criteria to get collections.

Use Service Contract (SearchCriteriaBuilder class)

For example, If you want to fetch the customers with filters, you can use the below code.

<?php

namespace CodextblogDemoHelper;

use MagentoFrameworkApiFilterBuilder;
use MagentoFrameworkApiSearchFilterGroupBuilder;
use MagentoFrameworkApiSearchCriteriaBuilder;
use MagentoCustomerApiCustomerRepositoryInterface;

class Customer extends AbstractHelper
{
    /**
     * @var CustomerRepositoryInterface
     */
    protected $customerRepository;

    /**
     * @var SearchCriteriaBuilder
     */
    protected $searchCriteriaBuilder;

    /**
     * @var FilterGroupBuilder
     */
    protected $filterGroupBuilder;

    /**
     * @var FilterBuilder
     */
    protected $filterBuilder;


    public function __construct(
        MagentoFrameworkAppHelperContext $context,
        CustomerRepositoryInterface $customerRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        FilterGroupBuilder $filterGroupBuilder,
        FilterBuilder $filterBuilder,
    ) {
    	parent::__construct($context);
    	$this->customerRepository = $customerRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->filterGroupBuilder = $filterGroupBuilder;
        $this->filterBuilder = $filterBuilder;
    }

    public function getCustomers() {
  		$filter1 = $this->filterBuilder->setField('member')
              ->setValue(1)
              ->setConditionType('neq')
              ->create();
          $filter2 = $this->filterBuilder->setField('member')
              ->setValue('')
              ->setConditionType('eq')
              ->create();

          $filterOr = $this->filterGroupBuilder
              ->addFilter($filter1)
              ->addFilter($filter2)
              ->create();
          $this->searchCriteriaBuilder->setFilterGroups([$filterOr]);

          $customers = $this->customerRepository->getList($this->searchCriteriaBuilder->create())->getItems();

          if ($customers) {
              foreach ($customers as $customer) {
                  //rest of the code
              }
          }
    }    
}

Use Resource Model’s Collection Factory Class

For example, If you want to fetch the category collection with filters, you can use the below code.

<?php

use MagentoCatalogModelResourceModelCategoryCollectionFactory;

class Index extends MagentoFrameworkViewElementTemplate
{
   /**
     * @var CollectionFactory
     */
   protected $_categoryCollection;

   public function __construct(
     Context $context,
     CollectionFactory $categoryCollection,
     array $data = []
   ) {
     $this->_categoryCollection = $categoryCollection;
     parent::__construct($context, $data);
   }

   public function getCategoryList()
    {
        return $this->_categoryCollection->create()->addAttributeToSelect('*')
            ->addAttributeToFilter('is_active', 1)
            ->addAttributeToFilter('include_in_menu', 1)
            ->addAttributeToFilter('parent_id', 4)
            ->setOrder('position', 'ASC');
    }

}

In conclusion, using a service contract to fetch any collection is the best way. Hope this code snippet helps you. Feel free to add a comment if you still have any doubts.

Written by

Chirag

Chirag is a Founder & eCommerce Technology Leader at MageArmy. He leads the eCommerce development team and drives the company’s technology vision, helping businesses build, scale, and optimize high-performing online stores. With years of experience in eCommerce development, he has worked with businesses across industries to solve complex technical challenges and create scalable digital commerce solutions. He loves sharing practical insights about eCommerce development, Magento, Adobe Commerce, and the latest trends in digital commerce to help online businesses grow and succeed.