0

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.

2
  • edit your attribute and set 'Used for Sorting in Product Listing' to yes if it is not set to yes
    – Mufaddal
    Commented Feb 4, 2014 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
    Commented Feb 4, 2014 at 13:21

2 Answers 2

1

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

1
  • Thanks :) You made my day :) Very Very thanks Commented Feb 4, 2014 at 14:01
1

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.

3
  • 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. Commented Feb 4, 2014 at 12:57
  • Could you please write the whole code of attribute creating? Commented Feb 4, 2014 at 13:00
  • I added codes to my question Commented Feb 4, 2014 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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