Showing posts with label filter. Show all posts
Showing posts with label filter. Show all posts

2014-07-22

Getting filterable attributes in category

Getting filterable attributes from layered navigation
$category = Mage::registry('current_category');
$products = $category->getProductCollection();

$appliedFilters = Mage::getSingleton('catalog/layer')->getFilterableAttributes();
foreach ($appliedFilters as $filter) {
  if ($value = $this->getRequest()->getParam($filter->getAttributeCode())) {
      $products->addFieldToFilter($filter->getAttributeCode(), $value);
  }
}
Method getFilterableAttributes returns all available attributes for filtering. Then in foreach we check if an attribute has been selected by user, we add it to the filter.

2014-06-08

Possible values for addAttributeToFilter

Collections in Magento have the method addAttributeToFilter. It filters entities.
This method takes an attribute code and a condition.
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('sku', 'test-sku');
$products->load();
Where 'test-sku' - value of condition.
If value of condition is integer or string - exact value will be filtered with 'eq' condition).
If value of condition is array - one of the following structures is expected:
array("from" => $fromValue, "to" => $toValue)
array("eq" => $equalValue)
array("neq" => $notEqualValue)
array("like" => $likeValue)
array("in" => array($inValues))
array("nin" => array($notInValues))
array("notnull" => $valueIsNotNull)
array("null" => $valueIsNull)
array("moreq" => $moreOrEqualValue)
array("gt" => $greaterValue)
array("lt" => $lessValue)
array("gteq" => $greaterOrEqualValue)
array("lteq" => $lessOrEqualValue)
array("finset" => $valueInSet)
array("regexp" => $regularExpression)
array("seq" => $stringValue)
array("sneq" => $stringValue)

2013-10-18

Условия фильтрации И и ИЛИ для коллекций magento

Добавить фильтр по атрибуту можно мотодом addFieldToFilter. Для фильтрации атрибутов с условием И надо использовать такой код:
->addFieldToFilter(array(
    array('attribute'=>'action','eq'=> 1),
))
->addFieldToFilter(array(
    array('attribute'=>'status','eq'=> 1),
))
а для условия ИЛИ такой:
->addFieldToFilter(
    array(
        array('attribute'=>'my_field1','eq'=>'my_value1'),
        array('attribute'=>'my_field2', 'eq'=>'my_value2')
    )
);