Pergunta

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.

Foi útil?

Solução

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

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top