2

I have an attribute in eav_attribute "group_id" for customer/address, I am trying to sort my collection by this attribute.What I am doing is.

$customer = Mage::getSingleton('customer/session')->getCustomer();
$addresses = $customer->getAddressesCollection();
$addresses->addAttributeToSort('group_id', 'DESC');

I have also tried following, but nothing is working.

$addresses->getSelect()->order('group_id', 'DESC');

And

$addresses->setOrder('group_id', 'DESC');
2
  • what u sure ''group_id' is an attribute of customer/address?
    – Amit Bera
    Oct 27 '16 at 7:51
  • Yes, it is has 'entity_type_id = 2' Oct 27 '16 at 7:55
1

If group_id is not an attribute of Customer address type.

So You cannot short address collection by group then that is not possible easily.

For this case,you need to add a left join with Customer/address resource collection Class Mage_Customer_Model_Resource_Address_Collection

public function addGroupId(){

         $this->joinAttribute('customer_group_id', 'customer/group_id', 'parent_id', 'entity_id', 'left');
  };

Then at customer model (app/code/core/Mage/Customer/Model/Customer.php) and modify getAddressesCollection() method

public function getAddressesCollection()
{
if ($this->_addressesCollection === null) {
    $this->_addressesCollection = $this->getAddressCollection()
        ->setCustomerFilter($this)
        ->addAttributeToSelect('*')
    ->addGroupId()  // add this line at Collection for adding groupid  customer address collection
        ->addAttributeToSort('customer_group_id'); // This line has been added for sorting
    foreach ($this->_addressesCollection as $address) {
        $address->setCustomer($this);
    }
}
return $this->_addressesCollection;
}

If group_id is an attribute of entity_type_id=2

if If group_id is an attribute of entity_type_id=2 mean it is an attribute of customer_address then only need to modify at getAddressesCollection().

Just need to add ->addAttributeToSort('group_id') as per as your share lik How do I sort all of the multiple shipping addresses associated with a customer

1
0

Instead of this code :

$addresses->getSelect()->order('group_id', 'DESC');

You can try this :

$addresses->getSelect()->order('group_id DESC');

It's working fine for me.

-1

Just replace:

$addresses->addAttributeToSort('group_id', 'DESC');

With:

$addresses->addAttributeToFilter('group_id', 'DESC');

Please tell me if it worked.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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