Question

I'm making an extension, that lets the user define companies and add addresses to them. The plan is to show those addresses to the customer according to the value of an attribute in the customer (eg. Company_Id). Thanks to the help of R.S I was able to create an observer (eav_collection_abstract_load_before) which is called before the customer/address collection is loaded.

Now at this point I want to add my addresses to the collection, so they're shown on the customer Addressbook page, on checkout etc.

I created a model in the same schema as the customer/address model and tried:

$companyAddress = Mage::getModel('myNamespace_MyModule/companyaddress')->load(1);    
$CustomerAddressCollection->addItem($companyAddress);

Here I get the error: Attempt to add invalid object

How can I merge these two collections?

Was it helpful?

Solution

You could rewrite the resource collection for address. customer/address_collection and adding a custom _afterLoad function.

In this function

protected function _afterLoad() {
    parent::_afterLoad(); // Just to be sure

    // Logic to get company address
    $companyAddress = Mage::getModel('myNamespace_MyModule/companyaddress')->load(1);

    // Get a new item and set the data from company address
    $item = $this->getNewEmptyItem();
    $item->setData($companyAddress->getData()); // Assuming you use the same kind of structure, do not set the id for the item

    $item->setData('company_address', $companyAddress); // Can come in handy

    // Add item
    $this->addItem($item);

    return $this;
}

Of coarse you need to keep in mind that a customer model save will save all addresses and there will be a possibility that your address will be saved to the database or after a edit in the account part.

But this is basically the answer to your question.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top