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

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(
          'value' => 4,
          'label' => Mage::helper('jetkharid_catalog')->__('In Stock')
          'value' => 3,
          'label' => Mage::helper('jetkharid_catalog')->__('Soon'),
          'value' => 2,
          'label' => Mage::helper('jetkharid_catalog')->__('Out Of Stock'),
          '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.

  • edit your attribute and set 'Used for Sorting in Product Listing' to yes if it is not set to yes – Mufaddal Feb 4 '14 at 13:05
  • 1
    I don't think there is a reason to downvote this. It is a legit question. I admit is not properly formulated but if you read carefully it makes sense. – Marius Feb 4 '14 at 13:21

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()) {
                ->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).

  • Thanks :) You made my day :) Very Very thanks – Pooya Saberian Feb 4 '14 at 14:01

Go to Attributes. Select your attribute for edit and set Used in Product Listing to yes.

Notice You can have the issue after adding new attribute. New attributes default value is not set to existing products. To do that go to product grid and in mass action select update attributes and set some value of your new attribute to all products.

  • I do this, but when I click on this attribute for sort in products list there is no difference between ASC or DESC direction! and this sort option doesn't work correct. – Pooya Saberian Feb 4 '14 at 12:57
  • Could you please write the whole code of attribute creating? – oleksii.svarychevskyi Feb 4 '14 at 13:00
  • I added codes to my question – Pooya Saberian Feb 4 '14 at 13:11

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.