如何从 eav_attribute 表获取实体(例如客户)的数据以显示在客户网格中以供管理员使用

发布于 2024-09-26 19:04:28 字数 2767 浏览 1 评论 0原文

我扩展了 Magento 的客户信息表单来存储客户的附加属性。我们将其称为“customer_referrer_id”。

我有一个“推荐人”角色,只能访问客户网格和订单网格。但是,我想限制推荐人只能看到网格中将 customer_referrer_id 设置为已登录的推荐人 ID 的客户。同样,对于订单,登录的推荐人只能看到由以下客户发出的订单:有 customer_referrer_id =login_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>

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

<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>

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();
    }  
}

I have extended Magento’s customer information form to store an additional attribute for customer. Lets call it ‘customer_referrer_id’.

I have a role ‘referrer ‘ who has access to customer grid and order grid only. But, I want to restrict a referrer to see only those customers in the grid who have the customer_referrer_id set as the referrer’s id who has logged in. Similarly for orders, the logged in referrer shall be able to see only those orders made by customers who have customer_referrer_id = loggedin_referrer_id.

I already know how to override a module and that I have to mainly override Adminhtml/Block/Customer/Grid::_prepareCollection and Adminhtml/Block/Sales/Order/Grid::_prepareCollection

I am using Magento 1.4.1.1

This is my module declaration file in 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>

And my modules config.xml in local/Myproject/Adminhtml/etc/ is as follows:

<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>

And

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();
    }  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2024-10-03 19:04:28

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

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

其中 $referrer_id 是您必须从登录用户检索的值。由于您似乎正在使用管理员用户(它们是与客户不同的实体),这是一种检索方式;

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

请注意,当前登录的管理员用户仅从数据库中看不出来,因此它不能是表连接。

另一方面,我会使用前端作为引荐来源网址而不是管理员。我会让新客户及其订单显示在推荐人的客户帐户中,类似于他们自己的“我的订单”页面。当然我不知道你还需要满足什么其他要求。

第二部分

覆盖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();
}

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

一个可能更简洁的方法是用您自己的集合类覆盖 sales/order_grid_collection 来执行所有这些操作。尽管它更好地遵循编码约定,但最终会增加工作量并且不再具有功能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文