0

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());
4
  • Why do you think it's bad to apply filters in such way? Jan 28, 2015 at 19:37
  • @Reindex'EmAll Wouldn't it be much more time intensive to apply a filter one after another in such a manner? Although, this may be a necessity case. Jan 28, 2015 at 19:39
  • 2
    No it would not. You're not doing query on DB until you load the collection. Jan 28, 2015 at 19:47
  • @Reindex'EmAll Good to know, take a look at what i've come up with. Jan 28, 2015 at 20:07

1 Answer 1

1

Updated Answer

Based on what we discussed this should be your solution:

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;
}

$_products = $this->filterProducts($this->getProductCollection(),$this->getRequest()->getParams());  
$_products->load();  

Another thing to note:

I don't know if you have made a block with this function getProductCollection but make sure it exists. If not you will have to make it or do this something like this:

$collection = Mage::getModel('catalog/product')->getCollection();
$_products = $this->filterProducts($collection,$this->getRequest()->getParams());

Old answer

I think this depends on what you are doing and what exactly you have in your $params array.

Here is one way. If the things you are trying to filter is the only thing in the params array then you can do something like:

foreach ($params as $param => $value) {
    $collection->addFieldToFilter($param, $value);
}

You should also check out addAttributeToFilter().

Hopefully this helps a little.

You can check out this link too http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections

7
  • There are more than the above parameters contained within the params array, so the conditional must take place. I've edited my code a bit to reflect your foreach loop mechanism. Tell me what you think and whether it can be further condensed. Jan 28, 2015 at 19:56
  • 1
    If you are only doing those conditions then I think your initial code is the better way to go. You would be adding more steps to do the same thing. You can think of it in terms of cost. An if else statement costs 1. A array assignment costs 1, so 1 X number of assignments. Then foreach loop, 1 X number of elements in the array. Too much @_@. I would only suggest the foreach loop if you are doing more than just the conditions or if your params array just contains the filters. If this is all you are doing then i agree with @Reindex 'Em All.
    – Aedonis
    Jan 28, 2015 at 20:01
  • 1
    Also to note: You must from my knowledge call addFieldToFilter() before you do $collection->load(). Magento does something called LazyLoading which means it doesn't do any SQL until you call $collection->load(). So I would suggest you not load the collection until you have filtered the collection.
    – Aedonis
    Jan 28, 2015 at 20:08
  • 1
    I think your solution now after the edit is correct. (without the space between $this-> and filterProducts())
    – Aedonis
    Jan 28, 2015 at 20:11
  • I'm unsure what i've done is correct then. Nowhere in this .phtml file is $collection->load() called. Jan 28, 2015 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.