0

I have a custom attribute in my magento store say "duration". and it has values like "2 Hours", "180 Minutes", "1 day" etc. for display on frontend.

But now I want to filter product collection by this attribute.

For example : I want to filter products with less than "1000 minutes" duration. Collection must return those products which has value in hours or days (according to hour and day calculation).

Is it possible to filter product collection using this attribute? If yes then How to apply filter on product collection?

1

I strongly recommend to normalize your data, so that the stored value is always in the same unit (e.g. minutes)

To display different values in the frontend, you can write a custom source model:

class Your_Module_Model_Source_Duration extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    public function getAllOptions()
    {
        return [
            ['value' => 120, 'label' => '2 hours'],
            ['value' => 180, 'label' => '180 minutes'],
            ['value' => 1440, 'label' => '1 day'],
        ];
    }
}

Then create/update the attribute via install script and specify the source model:

'source'        => 'your_module/source_duration',

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.