1

I want to sort the customer attribute address after sort_order. For some reason by default it doesn't work so I started to digging into the core. This function :

protected function _getFormAttributeCollection()
{
   return parent::_getFormAttributeCollection()
   ->addFieldToFilter('attribute_code', array('neq' => 'created_at'));
}

from Mage_Customer_Model_Form is responsible . If I add here this code ->setOrder('sort_order', 'ASC'); to my collection its working fine. Due to the fact that you don't touch the core, I want to fix this from an observer. I used this event: core_block_abstract_to_html_before and this is my function from my observer:

 $grid = $observer->getBlock();
 if ($grid instanceof  Mage_Adminhtml_Block_Widget_Form_Renderer_Fieldset_Element) {
   $collection = $observer->getEvent()->getCollection();
   $collection = $collection->getSelect()->order('sort_order', 'ASC');
// $collection->setOrder('sort_order', 'ASC');
   $observer->getEvent()->setCollection($collection);
 }

The fact is that $collection is always null. I don't know why ? Can someone give me an idea how to fix this ?

Thank you

2 Answers 2

0

It's not working because core_block_abstract_to_html_before is too broad, it's called every time a block is rendered. Even though you have a condition in your observer, it's still to broad, the collection is there, you are probably just looking at some other block that is loading on that page.

Solution:

Override Mage_Customer_Model_Form and add

return parent::_getFormAttributeCollection() 
    ->addFieldToFilter('attribute_code', array('neq' => 'created_at'))
    ->setOrder('sort_order', 'ASC');

To the _getFormAttributeCollection() method.

7
  • It is about this collection Mage::getResourceModel('customer/form_attribute_collection') . IF you check that function that I mentioned _getFormAttributeCollection in the core. May 26, 2017 at 10:51
  • If i use your collection I still get the wrong sorting order for my customer address attributes. May 26, 2017 at 10:52
  • If I use this $collection = Mage::getResourceModel('customer/form_attribute_collection') ->getCollection() ->addAttributeToSort('sort_order', Varien_Data_Collection::SORT_ORDER_ASC); Im getting this message: Call to undefined method Mage_Customer_Model_Resource_Form_Attribute_Collection::getCollection() May 26, 2017 at 10:55
  • Mage::getResourceModel('customer/form_attribute_collection') returns a collection, so you don't need the ->getCollection() Try using: $collection = Mage::getResourceModel('customer/form_attribute_collection')->addAttributeToSort('sort_order', Varien_Data_Collection::SORT_ORDER_ASC); But again, don't use the observer you created, it will be called way to often for what you're trying to achieve, override the block where the collection is called.
    – Radosav
    May 26, 2017 at 12:14
  • 1
    Or you can just override Mage_Customer_Model_Form and add ->setOrder('sort_order', 'ASC'); to return parent::_getFormAttributeCollection() ->addFieldToFilter('attribute_code', array('neq' => 'created_at'));
    – Radosav
    May 26, 2017 at 12:17
0

You can try

$addressAttributeCollection = Mage::getResourceModel('customer/address_attribute_collection');
$addressAttributeCollection->setOrder('sort_order', 'ASC');
1
  • nothing happens :) with or without this code: $observer->getEvent()->setCollection($addressAttributeCollection); still got the wrong sorting type . May 26, 2017 at 11:10

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.