将属性添加到集合的更好方法

Bry*_*uiz 2 magento

我重写了Mage/Adminhtml/Sales/Order/Grid.php并向prepareCollection添加了一些数据.这就是我将客户EAV属性campaign_id包含在集合中的方式,但它有点像hacky.我想知道是否有更好的方法.

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass());
    foreach ($collection as &$object){
        $customer = Mage::getModel('customer/customer')
            ->setId($object->getCustomerId())
            ->load();
        $object->setCampaignId($customer->getCampaignId());

    }
    $this->setCollection($collection);
    return parent::_prepareCollection();
}
Run Code Online (Sandbox Code Playgroud)

ben*_*rks 8

您需要在加载之前将客户记录中的数据加入订单集合中.

您可以在加载事件之前和之后观察集合.为了sales/order_grid_collection收集这些事件sales_order_grid_collection_load_beforesales_order_grid_collection_load_after-你要使用前者.您可以通过_before_load事件观察者访问该集合$observer->getOrderGridCollection().