Magento ver。 1.9.1.0:按数量和价格下降对产品进行排序

时间:2015-04-16 11:36:55

标签: mysql magento

我需要按数量和价格对产品进行分类,即所有类别的产品都应按最大数量列出。应该首先提供最大量的产品,然后列出较低数量的产品。我通过编写此代码实现了这一点 /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'
            )
        );

请帮帮我。提前致谢

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案:在按降序排序数量后添加此行代码==>           $ 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;
相关问题