Magento 版1.9.1.0:按数量和价格降序排列产品

Magento ver. 1.9.1.0 : Sort Products by Quantity and Price descending

我需要按数量和价格对产品进行排序 即所有类别的产品都应按最大数量列出。应先列出最大数量的产品,然后列出数量较少的产品。我通过在中编写这段代码来实现这一点 /app/code/core/Mage/Catalog/Model/Resource/Product/Collection.php

public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC)
{
    if ($attribute == 'position') {
   /**** Sorting for quantity starts***************/         
        //join the stock status index table for the current website and check if the product is saleable and the qtu.
        $select = $this->getSelect();
        $select->joinLeft(
            array('stock_qty' => $this->getTable('cataloginventory/stock_status')),
            'e.entity_id = stock_qty.product_id AND stock_qty.website_id='.Mage::app()->getWebsite()->getId(),
            array(
                'salable' => 'stock_qty.stock_status',
                'qty' => 'stock_qty.qty'
            )
        );
        //get the reversed sorting
        //if by position ascending, then you need to sort by qty descending and the other way around
        $reverseDir = ($dir == 'ASC') ? 'DESC' : 'ASC';
        //this is optional - it shows the in stock products above the out of stock ones independent if sorting by position ascending or descending
        $this->getSelect()->order('salable DESC');
        //sort by qty
        $this->getSelect()->order('qty '.$reverseDir);
        return $this;      

/**** Sorting for quantity ends ***************/  

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

    $storeId = $this->getStoreId();
    if ($attribute == 'price' && $storeId != 0) {
        $this->addPriceData();
        $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}");
        }
        else if (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);
}

现在我还需要按价格降序对产品进行排序,即数量随价格降序而降

Product  Quantity Price
-------  -------- -------
  A         100   9000
  B         100   8000
  C         89    7000
  D         80    6000 

我也尝试过转到 系统 > 配置 > 目录 > 产品列表排序方式 = 价格。但这对我没有帮助。所以我正在寻找解决方案,我认为解决方案是通过操作此处的代码

$select = $this->getSelect();
        $select->joinLeft(
            array('stock_qty' => $this->getTable('cataloginventory/stock_status')),
            'e.entity_id = stock_qty.product_id AND stock_qty.website_id='.Mage::app()->getWebsite()->getId(),
            array(
                'salable' => 'stock_qty.stock_status',
                'qty' => 'stock_qty.qty'
            )
        );

请帮帮我。提前致谢

终于找到解决办法:在数量降序后添加这行代码 ==> $this->getSelect()->order('price DESC');

$select = $this->getSelect();
        $select->joinLeft(
            array('stock_qty' => $this->getTable('cataloginventory/stock_status')),
            'e.entity_id = stock_qty.product_id AND stock_qty.website_id='.Mage::app()->getWebsite()->getId(),
            array(
                'salable' => 'stock_qty.stock_status',
                'qty' => 'stock_qty.qty'
            )
        );
        //get the reversed sorting
        //if by position ascending, then you need to sort by qty descending and the other way around
        $reverseDir = ($dir == 'ASC') ? 'DESC' : 'ASC';
        //this is optional - it shows the in stock products above the out of stock ones independent if sorting by position ascending or descending
        $this->getSelect()->order('salable DESC');

        //sort by qty
        $this->getSelect()->order('qty '.$reverseDir);
        $this->getSelect()->order('price DESC');
        return $this;