1

I found this sollution for filtering multiple multi-select attribute values.

$data = THE ARRAY OF THE CHECKBOX VALUES;

$filter = array();
foreach ($data as $value) {
    $filter[] = array(
        'attribute' => $attribute->getAttributeCode(),
        'finset'    => $value
    );
}

if (count($filter) > 0) {
    $collection->addAttributeToFilter($filter);
}

I need to filter the output alphabetically. For example my attribute looks like this:

Label     Value
apple     1051
banana    1052
pear      1053
melon     1054

and I want to sort it not on values but on labels.

1

I solved it by sorting on sort_order

if (count($filter) > 0) {
    $collection->addAttributeToFilter($filter);
    $collection->getSelect()->reset(Zend_Db_Select::ORDER);

    $dir = Varien_Db_Select::SQL_ASC;
    $dir = strtoupper($dir);

    $collection->getSelect()
        ->joinLeft('eav_attribute_option AS eao',
            "eao.option_id = e.my_attribute",
            array("sort_order" => 'eao.option_id')
        )
        ->order(new Zend_Db_Expr('eao.sort_order '.$dir));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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