3

We want to override the product collection model class(Magento\Catalog\Model\ResourceModel\Product\Collection.php)

We flow the below process to override the addAttributeToSort method but not working.

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <preference for="Magento\Catalog\Model\ResourceModel\Product\Collection" type="MyModule\SortByQty\Model\ResourceModel\Product\Collection" />
</config>

Collection.php

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

namespace MyModule\SortByQty\Model\ResourceModel\Product;


class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
{
    public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC)
    {
        if ($attribute == 'position') {
            if (isset($this->_joinFields[$attribute])) {
                $this->getSelect()->order($this->_getAttributeFieldName($attribute) . ' ' . $dir);
                return $this;
            }
            if ($this->isEnabledFlat()) {
                $this->getSelect()->order("cat_index_position {$dir}");
            }
            // optimize if using cat index
            $filters = $this->_productLimitationFilters;
            if (isset($filters['category_id']) || isset($filters['visibility'])) {
              $this->getSelect()->joinLeft(
            array('_inventory_table'=>'cataloginventory_stock_item'),
            "_inventory_table.product_id = e.entity_id ",
            array('qty')
        )->order(array('_inventory_table.qty DESC'));
            } else {
                $this->getSelect()->order('e.entity_id ' . $dir);
            }

            return $this;
        } elseif ($attribute == 'is_saleable') {
            $this->getSelect()->order("is_saleable " . $dir);
            return $this;
        }

        $storeId = $this->getStoreId();
        if ($attribute == 'price' && $storeId != 0) {
            $this->addPriceData();
            if ($this->_productLimitationFilters->isUsingPriceIndex()) {
                $this->getSelect()->order("price_index.min_price {$dir}");
                return $this;
            }
        }

        if ($this->isEnabledFlat()) {
            $column = $this->getEntity()->getAttributeSortColumn($attribute);

            if ($column) {
                $this->getSelect()->order("e.{$column} {$dir}");
            } elseif (isset($this->_joinFields[$attribute])) {
                $this->getSelect()->order($this->_getAttributeFieldName($attribute) . ' ' . $dir);
            }

            return $this;
        } else {
            $attrInstance = $this->getEntity()->getAttribute($attribute);
            if ($attrInstance && $attrInstance->usesSource()) {
                $attrInstance->getSource()->addValueSortToCollection($this, $dir);
                return $this;
            }
        }

        return parent::addAttributeToSort($attribute, $dir);
    }
}

Please, can you help on above issue? any references and suggestion are highly appreciated.

7
  • Pls check here community.magento.com/t5/Programming-Questions/…
    – Abdul
    Jan 23, 2018 at 13:23
  • Pls check here: magento.stackexchange.com/questions/169601/…
    – Abdul
    Jan 23, 2018 at 13:25
  • @Abdul I have already tried the above shared link but not working for addAttributeToSort method.Please let me know if any other suggestion. Jan 23, 2018 at 13:28
  • wich functionality you want to customize in product collection ex:sort ,filter etc Jan 23, 2018 at 13:47
  • @BirjitsinhZala we want to override the addAttributeToSort method to sort by quantity I have already added the code inside the method.it's working on core file.But unable to override. Jan 23, 2018 at 13:50

2 Answers 2

2

Kindly use plugin for this.

put below code in Namespace/Module/etc/di.xml

<type name="Magento\Catalog\Model\ResourceModel\Product\Collection">
    <plugin name="product-collection-sorting" type="Namespace\Module\Plugin\Product\Collection" sortOrder="1"/>
</type>

Create Namespace\Module\Plugin\Product\Collection.php and pasted below code in your file:

<?php
namespace Namespace\Module\Plugin\Product;

class Collection
{

    public function afterAddAttributeToSort(\Magento\Catalog\Model\ResourceModel\Product\Collection $subject, $result)
    {
         // do your stuff here
        return $result;
    }
}

Hope this will help you....!

0
0

You need to override the block Magento\Catalog\Block\Product\ProductList\Toolbar

put below code in Namespace/Module/etc/di.xml

<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
        <plugin name="cutom_productlimit" type="Namespace\Module\Plugin\Product\ProductList\Toolbar" />
</type>

Create plugin file Namespace/Module/Plugin/Product/Toolbar.php

 <?php 
namespace Namespace\Module\Plugin\Product\ProductList;    
class Toolbar
{

   public function aroundSetCollection(
        \Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
        \Closure $proceed,
        $collection
    ) {
        $CurrentDirection = $subject->getCurrentDirection()? $subject->getCurrentDirection(): 'DESC';
         // do your stuff here
        return $result;
    }
}

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.