如何从eav_attribute表获取实体(例如客户)的数据,以显示在Customer Grid for admin中

Oza*_*ray 5 magento e-commerce

我已经扩展了Magento的客户信息表,为客户存储了一个额外的属性.让我们称之为'customer_referrer_id'.

我有一个角色'推荐人',他只能访问客户网格和订单网格.但是,我想限制一个引荐来者只看到网格中那些将customer_referrer_id设置为已登录的引用者ID的客户.同样,对于订单,登录的引用者应该只能看到客户提出的那些订单.拥有customer_referrer_id = loggedin_referrer_id.

我已经知道如何覆盖模块,我必须主要覆盖Adminhtml/Block/Customer/Grid :: _ prepareCollection和Adminhtml/Block/Sales/Order/Grid :: _ prepareCollection

我正在使用Magento 1.4.1.1

这是app/etc/modules/Myproject_Adminhtml中的模块声明文件

<?xml version="1.0"?>

<config>
    <modules>
        <Myproject_Adminhtml>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Sales />
            </depends>
        </Myproject_Adminhtml>
    </modules>
</config>
Run Code Online (Sandbox Code Playgroud)

我在local/Myproject/Adminhtml/etc /中的模块config.xml如下:

<config>
    <modules>
        <Myproject_Adminhtml>
            <version>1.0.0</version>
        </Myproject_Adminhtml>    
    </modules>

    <global>
          <blocks>
            <adminhtml>
                <rewrite>
                <sales_order_grid>Myproject_Adminhtml_Block_Sales_Order_Grid</sales_order_grid>
                <customer_grid>Myproject_Adminhtml_Block_Customer_Grid</customer_grid>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
</config>
Run Code Online (Sandbox Code Playgroud)

class Myproject_Adminhtml_Block_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{
    protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('customer/customer_collection')
        ->addNameToSelect()
        ->addAttributeToSelect('email')
        ->addAttributeToSelect('created_at')
        ->addAttributeToSelect('group_id')
        ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
        ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
        ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
        ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
        ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');    

        $this->setCollection($collection);

        $referrer_id = Mage::getSingleton('admin/session')->getUser()->getId();
        Mage::log('Logged in admin has id: ' . $referrer_id);

        return parent::_prepareCollection();
    }  
}
Run Code Online (Sandbox Code Playgroud)

clo*_*eek 5

我的第一次尝试是(对于提到的两个文件),

$collection->addAttributeToFilter('customer_referrer_id', $referrer_id);
Run Code Online (Sandbox Code Playgroud)

$referrer_id您必须从登录用户中检索的值在哪里.由于您似乎正在使用管理员用户 - 这是与客户分开的实体 - 这是一种检索方式;

$referrer_id = Mage::getSingleton('admin/session')->getUser()->getId();
Run Code Online (Sandbox Code Playgroud)

请注意,当前登录的管理员用户不会仅从数据库中显而易见,因此它不能是表连接.

在另一点上,我会使用前端而不是管理员.我会在推荐人的客户帐户中显示新客户及其订单,类似于他们自己的"我的订单"页面.当然,我不知道你还有什么其他要求.

第二部分

覆盖Mage_Adminhtml_Block_Sales_Order_Grid::_prepareCollection()看起来像这样:

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();
}
Run Code Online (Sandbox Code Playgroud)

这是必要的,因为原始表sales/order_grid是平的,而不是实体集合,因此不能与属性连接.上面的工作相反,从客户集合开始,然后加入平面表.

一个可能更简洁的方法是覆盖sales/order_grid_collection你自己的集合类,它执行所有这些.虽然它更符合编码惯例,但它最终会起作用,而且不再具有功能性.

  • 谢谢你有趣的问题Ozair.我非常喜欢这个挑战.为了利用代码格式,我已经为订单网格问题添加了答案. (2认同)