user1597438
user1597438

Reputation: 2221

Show price range in homepage in Magento

I'm trying to show price range in the sidebar of my Magento site in the homepage. However, by default Magento has set category filters for the price range so if there is no category id, then the price range wouldn't work. Is there any way of implementing this function?

I've come up with the idea of creating a new page to display the list of products in. The thing is I have no idea how I can set the price range on my filter. This is the base idea I had in mind:

$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('price_to', '4000')
    ->addAttributeToFilter('price_from', '1000');

Then display the products matching the criteria but for obvious reasons, I can't use price_to and price_from. Can someone please point me to the right direction? Thanks in advance.

Upvotes: 1

Views: 1643

Answers (1)

user1597438
user1597438

Reputation: 2221

So I've done a lot of research with no ideal result for my case so I've created a new phtml file similar to list.phtml where I call the product collection with filters like so:

//this is dirty but this is the fastest solution I came up with
$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; 

if(false != strpos($url, 'to=500')):
$_below = 500;
elseif(false != strpos($url, 'to=1000')):
$_to = 1000;
$_from = 500;
elseif(false != strpos($url, 'to=2000')):
$_to = 2000;
$_from = 1001;
elseif(false != strpos($url, 'from=2001')):
$_above = 2000;
endif;

if(false != strpos($url, 'to=500')):
$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('price', array(
        'lteq' => $_below));
elseif(false != strpos($url, 'from=2001')):
$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('price', array(
        'gteq' => $_above));
else:
$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('price', array(
        'from' => $_from,
        'to' => $_to)); 
endif;

Where I set $_from and $_to depending on which range link is selected. I then call the custom.phtml in a CMS page I added specifically for it using:

{{block type="catalog/product_list" template="catalog/layer/customrange.phtml"}}

Then, on the sidebar, I added the necessary ranges (hardcoded) like so:

<ul><li><a href="<?php echo $this->getUrl('custom-page').'?from=&to=500'; ?>">below 500</a></li></ul> 

So I now have price range without category filters. It's not perfect I know and it needs working on given that the links on my sidebar for the range are hard-coded and I'm having a number of problems with the paging and toolbar (truth be told, it doesn't work at all) but this is the quickest solution I could come up with and it works. If someone has a better solution, please feel free to let me know and hope this helps someone else.

Upvotes: 1

Related Questions