I have Magento 2.4.2.

I have written a custom code to add A-Z & Z-A filters on category page.

I have added dependency injection using below code

<preference for="Magento\Catalog\Block\Product\ProductList\Toolbar" type="Vendor\ModuleName\Plugin\Product\ProductList\Toolbar" />

In below file, I have overridden setCollection function


public function setCollection($collection)
        $this->_collection = $collection;

        $limit = (int)$this->getLimit();
        if ($limit) {

        if ($this->getCurrentOrder()) {
            switch ($this->getCurrentOrder()) {
            case 'az':
                    $this->_collection->setOrder('name', 'asc');
                case 'za':
                    $this->_collection->setOrder('name', 'desc');


The issue is that this function is called twice, the first time it returns proper result (used X-debug to debug query of collection). And the second time it is called, it messes up the query of the collection by adding some entity_id filter and sorters.

Due to which the collection expected isn't coming.

What could be the reason of above function getting called twice.

  • what other code you have done for this one, please share that one as well. Jul 13, 2021 at 6:13

1 Answer 1


The idea is that when you run it for the first time you will create some variable into the session or cookie or anywhere else you can call it. The second time you run it, you check that if the original variable exists, it will not run into your custom function anymore.

Your Answer

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

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