Below is a function that i'm unsure how to implement. I need to conditionally filter this collection. Is there a way without continuously applying one filter after one another? Thanks
public function filterProducts($collection, $params)
{
if ($model = $params['product_model']) {
//filter collection by model
}
if ($productType = $params['product_type']) {
//filter collection by productType
}
if ($brand = $params['brand_name']) {
//filter collection by brand
}
if ($year = $params['product_application_year']) {
//filter collection by year
}
elseif($year = Mage::getModel('core/cookie')->get('cookie')){
//filter collection by year
}
return $collection;
}
Below is what i've come up with thus far.
public function filterProducts($collection, $params)
{
if ($params['product_model']) {
$collection->addAttributeToFilter('product_model',$params['product_model']);
}
if ($params['product_type']) {
$collection->addAttributeToFilter('product_type',$params['product_type']);
}
if ($params['brand_name']) {
$collection->addAttributeToFilter('brand_name',$params['brand_name']);
}
if ($params['product_application_year']) {
$collection->addAttributeToFilter('product_application_year',$params['product_application_year']);
} elseif (Mage::getModel('core/cookie')->get('myMustang')) {
$collection->addAttributeToFilter('product_application_year',$params['product_application_year']);
}
return $collection;
}
This is how i'm calling this function:
$_products = $this->filterProducts($this->getProductCollection(),$this->getRequest()->getParams());