2015-04-16 46 views
0

我需要按數量和價格對產品進行分類即所有類別的產品應按最大數量列出。產品的最大數量應該是第一位的,然後應該列出較低數量的產品。我已經在 /app/code/core/Mage/Catalog/Model/Resource/Product/Collection.phpMagento ver。 1.9.1.0:按數量和價格排序產品

編寫這些代碼
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' 
      ) 
     ); 

請幫我。在此先感謝

回答

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;