How to get data for an entity (for example customer) from eav_attribute table to be shown in Customer Grid for admin

半城伤御伤魂 提交于 2019-11-29 10:51:51

My first attempt would be (for both files mentioned),

$collection->addAttributeToFilter('customer_referrer_id', $referrer_id);

Where $referrer_id is the value you must retrieve from the logged in user. Since you appear to be using admin users - which are separate entities from customers - this is one way of retrieving;

$referrer_id = Mage::getSingleton('admin/session')->getUser()->getId();

Note the currently logged in admin user is not apparent from the database alone so it cannot be a table join.

On another point I would use the frontend for referrers instead of the admin. I would have the new customers and their orders shown in the referrer's customer account, similar to their own "My Orders" page. Of course I don't know what other requirements you have to attend to.

Second part

Override Mage_Adminhtml_Block_Sales_Order_Grid::_prepareCollection() to look like this:

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel('customer/customer_collection');
    $collection->getSelect()->reset('columns'); // remove all customer columns
    $collection->addAttributeToFilter('entity_id', $referrer_id);
    $collection->joinTable('sales/order_grid', 'customer_id=entity_id', array('*'));

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

This is necessary because the original table sales/order_grid is flat, not an entity collection, and so cannot be joined with attributes. The above works in reverse, by starting with a customer collection and then joining the flat table after.

A possibly neater method would be to override sales/order_grid_collection with your own collection class which performs all this. Although it better follows coding conventions it is more work and no more functional in the end.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!