Magento Enterprise – Sort Product Collection by Custom Integer Attribute

magento-enterpriseproduct-attributeproduct-collectionsorting

I created a custom_stock_status attribute in my module with 'int' backend type and four options in resource.

array(
4 => 'In Stock',
3 => 'Soon',
2 => 'Out Of Stock',
1 => 'Discontinued',
);

The code I use for this attribute creation:

class Jetkharid_Catalog_Model_Resource_Setup extends Mage_Eav_Model_Entity_Setup
{
  public function getDefaultEntities()
  {
    return array(
      'catalog_product'             => array(
        'entity_model'                   => 'catalog/product',
        'attribute_model'                => 'catalog/resource_eav_attribute',
        'table'                          => 'catalog/product',
        'additional_attribute_table'     => 'catalog/eav_attribute',
        'entity_attribute_collection'    => 'catalog/product_attribute_collection',
        'attributes'                     => array(
          'jetkharid_stock_status'             => array(
            'type'                       => 'int',
            'label'                      => 'Stock Status',
            'input'                      => 'select',
            'source'                     => 'jetkharid_catalog/product_attribute_source_stock_status',
            'required'                   => true,
            'is_configurable'            => false,
            'global'                     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
            'group'                      => 'General Information',
            'searchable'                 => true,
            'used_in_product_listing'    => true,
            'user_defined'               => false,
            'default'                    => 4,
          ),
        ),
      ),
    );
  }
}

And this is my source for this attribute options:

class Jetkharid_Catalog_Model_Product_Attribute_Source_Stock_Status extends Mage_Eav_Model_Entity_Attribute_Source_Abstract 
{
  public function getAllOptions()
  {
    if (!$this->_options) {
      $this->_options = array(
        array(
          'value' => 4,
          'label' => Mage::helper('jetkharid_catalog')->__('In Stock')
        ),
        array(
          'value' => 3,
          'label' => Mage::helper('jetkharid_catalog')->__('Soon'),
        ),
        array(
          'value' => 2,
          'label' => Mage::helper('jetkharid_catalog')->__('Out Of Stock'),
        ),
        array(
          'value' => 1,
          'label' => Mage::helper('jetkharid_catalog')->__('Discontinued'),
        )
      );
    }
    return $this->_options;
  }
}

Now I want sort my collection by this attribute, but it seems not working!

How should I do?!

Thanks guys.

Best Answer

For the custom source attributes you need to implement the method addValueSortToCollection in order to make it work.
This method is called in Mage_Catalog_Model_Resource_Product_Collection::addAttributeToSort
Look at this code in particular:

        $attrInstance = $this->getEntity()->getAttribute($attribute);
        if ($attrInstance && $attrInstance->usesSource()) {
            $attrInstance->getSource()
                ->addValueSortToCollection($this, $dir); 
            return $this;
        }

Use the method in this class as an example Mage_Catalog_Model_Product_Status.
That is used when sorting the products by status.
I know it's long an confusing but I think you can basically use the same code as in that class. (not sure though).

Related Topic