Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento sort by entity_id

Tags:

magento

Does anyone know how to make entity_id visible on the frontend as a sortable attribute? I went to manage attributes and I can't add entity_id as an attribute as it says

"The attribute code 'entity_id' is reserved by system. Please try another attribute code"

So I tried to do a search in the whole database I had Magento on using the following SQL CMD:

select attribute_id from eav_attribute where attribute_code = 'updated_at';

and it returned zero results, I tried other attributes and those showed...

Does anyone know how I can add entity_id as an attribute, as I can't even make it visible because I have no idea what the attribute_id # is even while searching the whole database for that value.

Here is the code I use to make a attribute visible on the admin section of magento:

UPDATE `catalog_eav_attribute` 
SET `is_visible` = '1' 
WHERE `catalog_eav_attribute`.`attribute_id` = 105;

I've tried searching high and low for days, and trying different variations - so stuck at this point, any help would be great.

I'm using Magento Enterprise 12.2 if that helps, but to be honest the DB is not much different than the community version the only differences are the added modules but when it comes to products it's pretty much the same.

like image 252
rrrcode Avatar asked Jun 21 '26 08:06

rrrcode


1 Answers

This is surprisingly nasty - I can't see a way to do this simply in the DB, as entity_id isn't a standard attribute (it's really just a key field) and the core code repeats the same logic in three place (urgh).

It's also changed a lot since the older versions of Magento, so many of the tutorials and forum posts on this no longer apply.

What you can do is add "entity_id" in as a new sorting option, similar to the way "Best Value" exists as a sorting option. In the following example I've labelled it "Newest"

The usual caveats apply: You should do this in an extension (or at the least in /local/ overrides) but the three core file methods that need overriding in Community Edition 1.7 are:

Mage_Adminhtml_Model_System_Config_Source_Catalog_ListSort

public function toOptionArray()
{
    $options = array();
    $options[] = array(//benz001
            'label' => Mage::helper('catalog')->__('Newest'),
            'value' => 'entity_id'
        );  //end benz001       
    $options[] = array(
        'label' => Mage::helper('catalog')->__('Best Value'),
        'value' => 'position'
    );
    foreach ($this->_getCatalogConfig()->getAttributesUsedForSortBy() as $attribute) {
        $options[] = array(
            'label' => Mage::helper('catalog')->__($attribute['frontend_label']),
            'value' => $attribute['attribute_code']
        );
    }
    return $options;
}

Then Mage_Catalog_Model_Category_Attribute_Source_Sortby

    /**
 * Retrieve All options
 *
 * @return array
 */
public function getAllOptions()
{
    if (is_null($this->_options)) {
        $this->_options = array(
        array(//benz001
            'label' => Mage::helper('catalog')->__('Newest'),
            'value' => 'entity_id'
        ),  //end benz001
        array(
            'label' => Mage::helper('catalog')->__('Best Value'),
            'value' => 'position'
        ));
        foreach ($this->_getCatalogConfig()->getAttributesUsedForSortBy() as $attribute) {
            $this->_options[] = array(
                'label' => Mage::helper('catalog')->__($attribute['frontend_label']),
                'value' => $attribute['attribute_code']
            );
        }
    }
    return $this->_options;
}

And then Mage_Catalog_Model_Config

    public function getAttributeUsedForSortByArray()
{
    $options = array(
        'entity_id' => Mage::helper('catalog')->__('Newest'),   //benz001   
        'position'  => Mage::helper('catalog')->__('Position'),             
    );
    foreach ($this->getAttributesUsedForSortBy() as $attribute) {
        /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
        $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
    }

    return $options;
}

With those in place, flush the cache and refresh and you can then select to sort by Newest/entity_id in the config and on the category page and on the frontend.

Note: even if you have caching turned off it's still a good idea to flush the cache - parts of the admin are cached even when caching is disabled.

like image 69
benz001 Avatar answered Jun 25 '26 18:06

benz001



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!