you cannot modify the attribute created_at
in the backend.
So you need to create a module that just activates the created_at
attribute for sorting.
For this you will need the following files:
app/etc/modules/[Namespace]_[Module].xml
- the declaration file
<?xml version="1.0"?>
<config>
<modules>
<[Namespace]_[Module]>
<codePool>local</codePool>
<active>true</active>
<depends>
<Mage_Catalog />
</depends>
</[Namespace]_[Module]>
</modules>
</config>
app/code/local/[Namespace]/[Module]/etc/config.xml
- the configuration file
<?xml version="1.0"?>
<config>
<modules>
<[Namespace]_[Module]>
<version>1.0.0</version>
</[Namespace]_[Module]>
</modules>
<global>
<resources>
<[namespace]_[module]_setup>
<setup>
<module>[Namespace]_[Module]</module>
<class>Mage_Catalog_Model_Resource_Setup</class> <!-- Make sure you use this class for setup -->
</setup>
</[namespace]_[module]_setup>
</resources>
</global>
</config>
app/code/local/[Namespace]/[Module]/sql/[namespace]_[module]_setup/install-1.0.0.php
- the install script
<?php
$this->updateAttribute('catalog_product', 'created_at', 'frontend_label', 'Date Added'); //Set a label to the attribute because by default it does not have one
$this->updateAttribute('catalog_product', 'created_at', 'used_for_sort_by', '1'); //mark the attribute as used for sorting.
Clear the cache and refresh any page. Now you should have the Date Added
(created_at
) attribute available for sorting in any category.
If you are referring to the product created_at
and updated_at
attributes, then simply commenting out the ->addVisibleFilter()
in the Catalog Product Attribute Grid in the admin and simply set the Search and Sort fields to Yes, and re-index, you'll notice the date(s) are now in the catalogsearch_fulltext
table which the Enterprise Solr will recognize and add the data to its doc records as well.
NOTE: Don't forgot to remove the core code change or do it via local code pool obviously if you like.
class Mage_Adminhtml_Block_Catalog_Product_Attribute_Grid extends Mage_Eav_Block_Adminhtml_Attribute_Grid_Abstract
{
/**
* Prepare product attributes grid collection object
*
* @return Mage_Adminhtml_Block_Catalog_Product_Attribute_Grid
*/
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('catalog/product_attribute_collection');
//->addVisibleFilter();
$this->setCollection($collection);
return parent::_prepareCollection();
}
After this you'll want to let Solr know its a sortable metadata field.
Enterprise/Search/Model/Adapter/Abstract.php you'll notice:
/**
* Store common Solr metadata fields
* All fields, that come up from search engine will be filtered by these keys
*
* @var array
*/
protected $_usedFields = array('sku', 'visibility', 'in_stock');
You may want to also look over the method: _prepareIndexProductData
in
Enterprise/Search/Model/Adapter/Abstract.php
/**
* Prepare index data for using in search engine metadata.
* Prepare fields for advanced search, navigation, sorting and fulltext fields for each search weight for
* quick search and spell.
*
* @param array $productIndexData
* @param int $productId
* @param int $storeId
*
* @return array|bool
*/
protected function _prepareIndexProductData($productIndexData, $productId, $storeId)
I've only tested the ability to edit the "non-visible" attributes and confirmed the addition to the fulltext table, I don't have Solr running locally at the moment to test it, but in theory should work without any need to mirror redundant data.
Hope this helps!
Best Answer
setDefaultSort()
ofgrid class of a module
is set grid order field in magento.As you want the change currenr grid sort order of product then you need go
Mage_Adminhtml_Block_Catalog_Product_Grid
.Here you see magento already set'entity_id'
as default sort order.You need change this.
So rewrite class the class
Mage_Adminhtml_Block_Catalog_Product_Grid
and do whatever you want.config.xml:
Class is:
Edit:
As per as your coment you want to add created date on product grid then you can the the below code: