2

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

Vendor\ModuleName\Plugin\Product\ProductList\Toolbar

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

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

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


                    ......

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.

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

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, privacy policy and cookie policy

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