由于没有人帮助我,我自己想出了答案。
首先,我将说明我的任务是什么。
假设我有 10 个价格范围在 100-200 的产品,12 个在 500-600 和 1 个 X 类别的 10000-20000 产品。使用 magento 的默认价格范围,它显示为两个范围,即 0-10000 和 10000-200000,这对客户来说没有多大用处。
我必须将范围设为 100-200,500-600 和 1000<
我会在这里发布我所有的代码。
当我关注其他自定义范围的帖子时,他们中的大多数人只考虑分层导航的显示。但是在这里我已经考虑在单击最终范围(即 1000<)时显示产品
请注意,LMage 是本地->LMage 文件夹,而且我是 Magento 的新蜜蜂;)。
如果您找到更好的方法,请在此处发布。
class LMage_CatalogIndex_Model_Mysql4_Price extends Mage_CatalogIndex_Model_Mysql4_Price {
public function getCount($range, $attribute, $entitySelect) {
$select = clone $entitySelect;
$select->reset(Zend_Db_Select::COLUMNS);
$select->reset(Zend_Db_Select::ORDER);
$select->reset(Zend_Db_Select::LIMIT_COUNT);
$select->reset(Zend_Db_Select::LIMIT_OFFSET);
$select->join(array('price_table' => $this->getMainTable()), 'price_table.entity_id=e.entity_id', array());
$response = new Varien_Object();
$response->setAdditionalCalculations(array());
if ($attribute->getAttributeCode() == 'price') {
$select->where('price_table.customer_group_id = ?', $this->getCustomerGroupId());
$args = array(
'select' => $select,
'table' => 'price_table',
'store_id' => $this->getStoreId(),
'response_object' => $response,
);
Mage::dispatchEvent('catalogindex_prepare_price_select', $args);
}
$fields = array('count' => 'COUNT(DISTINCT price_table.entity_id)', 'range' => "FLOOR(((price_table.value" . implode('', $response->getAdditionalCalculations()) . ")*{$this->getRate()})/{$range})+1");
$select->from('', $fields)
->group('range')
->where('price_table.website_id = ?', $this->getWebsiteId())
->where('price_table.attribute_id = ?', $attribute->getId());
$result = $this->_getReadAdapter()->fetchAll($select);
$counts = array();
foreach ($result as $row) {
if ($row['range'] >= 11) {
$counts[11] = isset($counts[11])?$row['count']+$counts[11]:$row['count'];
} else {
$counts[$row['range']] = $row['count'];
}
}
return $counts;
}
public function applyFilterToCollection($collection, $attribute, $range, $index, $tableName = 'price_table') {
/**
* Distinct required for removing duplicates in case when we have grouped products
* which contain multiple rows for one product id
*/
$collection->getSelect()->distinct(true);
$tableName = $tableName . '_' . $attribute->getAttributeCode();
$collection->getSelect()->joinLeft(
array($tableName => $this->getMainTable()), $tableName . '.entity_id=e.entity_id', array()
);
$response = new Varien_Object();
$response->setAdditionalCalculations(array());
$collection->getSelect()
->where($tableName . '.website_id = ?', $this->getWebsiteId())
->where($tableName . '.attribute_id = ?', $attribute->getId());
if ($attribute->getAttributeCode() == 'price') {
$collection->getSelect()->where($tableName . '.customer_group_id = ?', $this->getCustomerGroupId());
$args = array(
'select' => $collection->getSelect(),
'table' => $tableName,
'store_id' => $this->getStoreId(),
'response_object' => $response,
);
Mage::dispatchEvent('catalogindex_prepare_price_select', $args);
}
$collection->getSelect()->where("(({$tableName}.value" . implode('', $response->getAdditionalCalculations()) . ")*{$this->getRate()}) >= ?", ($index - 1) * $range);
if($index<=10){
$collection->getSelect()->where("(({$tableName}.value" . implode('', $response->getAdditionalCalculations()) . ")*{$this->getRate()}) < ?", $index * $range);
}
return $this;
}
public function getCategoryProductPrices($attribute = null, $entitySelect) {
$select = clone $entitySelect;
$select->reset(Zend_Db_Select::COLUMNS);
$select->reset(Zend_Db_Select::ORDER);
$select->reset(Zend_Db_Select::LIMIT_COUNT);
$select->reset(Zend_Db_Select::LIMIT_OFFSET);
$response = new Varien_Object();
$response->setAdditionalCalculations(array());
$select->join(array('price_table' => $this->getMainTable()), 'price_table.entity_id=e.entity_id', array());
if ($attribute->getAttributeCode() == 'price') {
$select->where('price_table.customer_group_id = ?', $this->getCustomerGroupId());
$args = array(
'select' => $select,
'table' => 'price_table',
'store_id' => $this->getStoreId(),
'response_object' => $response,
);
Mage::dispatchEvent('catalogindex_prepare_price_select', $args);
}
$select
->from('', "(price_table.value" . implode('', $response->getAdditionalCalculations()) . ")")
->where('price_table.website_id = ?', $this->getWebsiteId())
->where('price_table.attribute_id = ?', $attribute->getId());
return $this->_getReadAdapter()->fetchAll($select);
}
}
class LMage_CatalogIndex_Model_Price extends Mage_CatalogIndex_Model_Price{
public function getCategoryProductPrices($attribute, $entityIdsFilter){
return $this->_getResource()->getCategoryProductPrices($attribute, $entityIdsFilter);
}
}
class LMage_Catalog_Model_Layer_Filter_Price extends Mage_Catalog_Model_Layer_Filter_Price {
public function getPriceRange() {
$range = $this->getData('price_range');
if (is_null($range)) {
$productsprice = $this->getCategoryProductPricesArr();
$maxPrice = $this->getMaxPriceInt();
$maxPrice = $this->getMaxPriceOfMaxOccurenceRange($productsprice, $maxPrice);
$index = 1;
do {
$range = pow(10, (strlen(floor($maxPrice)) - $index));
$items = $this->getRangeItemCounts($range);
$index++;
} while ($range > self::MIN_RANGE_POWER && count($items) < 1);
$this->setData('price_range', $range);
}
return $range;
}
public function getMaxPriceOfMaxOccurenceRange($productsprice, $maxPrice) {
$rangeArr = array();
$i = 1;
$val = 0;
do {
$val = self::MIN_RANGE_POWER * $i - 1;
$rangeArr[$val] = 0;
$i *= 10;
} while ($maxPrice > $val);
foreach ($productsprice as $value) {
$rangeArr[pow(10, strlen(floor($value['value']))) - 1]+=1;
}
return array_search(max($rangeArr), $rangeArr);
}
public function getCategoryProductPricesArr() {
$productsprice = $this->getData('products_price_arr');
if (is_null($productsprice)) {
$productsprice = Mage::getSingleton('catalogindex/price')->getCategoryProductPrices(
$this->getAttributeModel(), $this->_getBaseCollectionSql()
);
$this->setData('products_price_arr', $productsprice);
}
return $productsprice;
}
/**
* Prepare text of item label
*
* @param int $range
* @param float $value
* @return string
*/
protected function _renderItemLabel($range, $value) {
$store = Mage::app()->getStore();
if ($value > 10) {
$fromPrice = $store->formatPrice(($value - 1) * $range);
//$toPrice = $store->formatPrice($value * $range);
return Mage::helper('catalog')->__('%s < ', $fromPrice);
}
$fromPrice = $store->formatPrice(($value - 1) * $range);
$toPrice = $store->formatPrice($value * $range);
return Mage::helper('catalog')->__('%s - %s', $fromPrice, $toPrice);
}