Reputation: 305
I know how Magento can filter prices by Range.
But, how can I filter for products with price X and higher? I dont want a top limit for the Price.
If you need a further explanation please ask.
A little example to be more clear. I want be able to add a price filter which supports for example from 200$ up to 800 000$
Upvotes: 2
Views: 4039
Reputation: 103
I use this code below for my filter.phtml. hope this help.
<?php
$getLabel = $_item->getLabel();
if (strpos($getLabel, 'price')!== false) :?>
<a class="multi-select unselected" href="<?php echo $this->urlEscape($_item->getUrl()) ?>">
<?php
$getValue = $_item->getValue();
$fitlerPrices = str_replace('-', ' - ', $getValue);
$file = basename($fitlerPrices);
$parts = explode("-", $file);
$getCurency = Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();
$priceBefore = $getCurency . number_format($parts[0], 0);
$priceAfter = $getCurency . number_format($parts[1], 0); ?>
<?php
if($i == $count){
//Last Item
echo '<span class="price">' . $priceBefore . ' and Above</span>';
}elseif($i <= 1){
//First Item
echo '<span class="price">Under ' . $priceAfter . '</span>';
}else{
echo '<span class="price">' . $priceBefore . ' - ' .$priceAfter . '</span>';
}
?>
</a>
<?php else :?>
<a class="multi-select unselected" href="<?php echo $this->urlEscape($_item->getUrl()) ?>"><?php echo $_item->getLabel(); ?></a>
<?php endif?>
Upvotes: 0
Reputation: 14182
Ugly answer: in order to apply a limit to price fields you need to revert to the (almost) plain old SQL.
First a little background.
Product collections use pre-calculated values from the price index table catalog_product_index_price
.
The index table gets joined in using the table alias price_index
when addPriceData()
is called on the collection.
Lets assume we have a product collection and a variable containing the lower price limit initialized as follows:
$lowerPriceLimit = 200;
/** @var $products Mage_Catalog_Model_Resource_Product_Collection */
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('name');
You probably want to test against the field final_price
because that is the value that will be used when a product is purchased (as compared to price
, special_price
or others).
This is an example how add the condition:
// Join the price_index table
$products->addPriceData();
// Apply price limit
$products->getSelect()->where('price_index.final_price >= ?', $lowerPriceLimit);
Here is an alternative. If you want to do it a little more the Magento way (that is use more complicated methods), use this:
$customerGroupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$websiteId = Mage::app()->getWebsite()->getId();
$products->joinField(
'filter_price', // field alias
'catalog/product_index_price', // table
'final_price', // real field name
'entity_id=entity_id', // primary condition
array( // additional conditions
'website_id' => $websiteId,
'customer_group_id' => $customerGroupId,
'final_price' => array('gteq' => $lowerPriceLimit)
)
);
If you also use addPriceData()
in this second way you will end up with a double inner join on the price index, but it will still work...
All rather low level, but on the up side of things, at least this is still pretty standard compliant SQL, so it should be rather upward compatible, too.
Maybe you can combine this with the answer from Sylvain to make it part of the layered navigation price range filter.
Upvotes: 2
Reputation: 1522
if you want like this
under $50
$50 - $99(5)
$100 - $199(3)
$200 - $299(10)
$300 - $499(2)
$500 - $699(17)
$700 - $899(1)
$900 or above(12)
You can get help from http://www.codegyan.com/2012/01/06/customize-filter-price-range-in-magento/
Upvotes: 0
Reputation: 2466
Here is an example on how you can create your own filter with some explanation. We use it to provide a filter for our free products, you will need to adapt to your situation.
You need to overwrite the class Mage_Catalog_Model_Layer_Filter_Price
In the code below, $index
define the index in the price range filter. If you have 4 price range, index 1 is the first and index 4 in the last one. $range
is the range of price, if you have a range = 100 and an index = 1, the price range will be between 1 to 100. If you have index = 4 and range = 1000, you will get product price range from 3000 USD to 4000 USD (USD is an example).
The code may be improved, you could for example do in the method _getItemsData()
, $data = parent::_getItemsData()
, then use $data to make your changes
class MyCompany_Catalog_Model_Layer_Filter_Price extends Mage_Catalog_Model_Layer_Filter_Price {
protected function _getItemsData()
{
$range = $this->getPriceRange();
$dbRanges = $this->getRangeItemCounts($range);
// Display an item Free Products to allow to select only them in the layer navigation block
if($count = $this->_getResource()->getCountFreeProducts($this)){
$data[] = array(
'label' => Mage::helper('catalog')->__('Free Products'),
'value' => '1,1',
'count' => $this->_getResource()->getCountFreeProducts($this) );
}else{
$data = array();
}
foreach ($dbRanges as $index=>$count) {
$data[] = array(
'label' => $this->_renderItemLabel($range, $index),
'value' => $index . ',' . $range,
'count' => $count,
);
}
return $data;
}
}
Upvotes: 0