想要从产品系列中删除(((price_table_price.value)* 1)<

时间:2012-05-30 10:56:06

标签: php magento

我对Magento很新。 (确实这是我的第一个任务)。如果你能帮助我,我将非常高兴。

我使用的是magento示例数据库,而Magento版本是1.3.2。

本地PC网址:http://magento.local/electronics/cell-phones.html?price=4,100

分类:Mage_Catalog_Block_Product_List

protected function _getProductCollection()
{
    if (is_null($this->_productCollection)) {
        $layer = Mage::getSingleton('catalog/layer');
        /* @var $layer Mage_Catalog_Model_Layer */
        if ($this->getShowRootCategory()) {
            $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
        }

        // if this is a product view page
        if (Mage::registry('product')) {
            // get collection of categories this product is associated with
            $categories = Mage::registry('product')->getCategoryCollection()
                ->setPage(1, 1)
                ->load();
            // if the product is associated with any category
            if ($categories->count()) {
                // show products from this category
                $this->setCategoryId(current($categories->getIterator()));
            }
        }

        $origCategory = null;
        if ($this->getCategoryId()) {
            $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
            if ($category->getId()) {
                $origCategory = $layer->getCurrentCategory();
                $layer->setCurrentCategory($category);
            }
        }
        $this->_productCollection = $layer->getProductCollection();

        $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

        if ($origCategory) {
            $layer->setCurrentCategory($origCategory);
        }
    }        
    return $this->_productCollection;
}

我需要从$ this-> _productCollection中删除(((price_table_price.value)* 1)< 400)。 无法弄清楚这是如何以及何时添加到产品集合中的。请帮助!!!

感谢!!!!

1 个答案:

答案 0 :(得分:0)

由于没有人帮助我,我自己想出答案。

首先,我将陈述我的任务。

假设我有一个产品10在100-200价格范围内,12在500-600和1产品在10000-20000 X类。使用magento的默认价格范围,它显示为0-10000和10000-200000两个范围,这对客户来说没什么用。

我必须将范围设为100-200,500-600和1000

我会在这里发布我的所有代码。

当我按照其他帖子进行自定义范围时,大多数人只考虑显示分层导航。但是,在点击最终范围(即1000<)

时,我已经考虑过显示产品

请注意LMage是local-> 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);
}