问题描述
我扩展了 Magento 的客户信息表单来存储客户的附加属性.我们称之为customer_referrer_id".
I have extended Magento’s customer information form to store an additional attribute for customer. Lets call it ‘customer_referrer_id’.
我有一个角色推荐人",他只能访问客户网格和订单网格.但是,我想限制推荐人只能看到网格中那些将 customer_referrer_id 设置为已登录的推荐人 ID 的客户.同样对于订单,登录的推荐人只能看到那些客户下的订单有 customer_referrer_id = login_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.
我已经知道如何覆盖一个模块,我必须主要覆盖 Adminhtml/Block/Customer/Grid::_prepareCollection 和 Adminhtml/Block/Sales/Order/Grid::_prepareCollection
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
我使用的是 Magento 1.4.1.1
I am using Magento 1.4.1.1
这是我在 app/etc/modules/Myproject_Adminhtml 中的模块声明文件
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>
我在local/Myproject/Adminhtml/etc/中的模块config.xml如下:
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>
还有
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();
}
}
推荐答案
我的第一次尝试是(对于提到的两个文件),
My first attempt would be (for both files mentioned),
$collection->addAttributeToFilter('customer_referrer_id', $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.
第二部分
覆盖 Mage_Adminhtml_Block_Sales_Order_Grid::_prepareCollection()
看起来像这样:
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();
}
这是必要的,因为原始表sales/order_grid
是扁平的,不是实体集合,因此不能与属性连接.上述工作相反,从客户集合开始,然后加入平面表.
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.
一个可能更简洁的方法是用您自己的集合类覆盖 sales/order_grid_collection
来执行所有这些.虽然它更好地遵循编码约定,但它最终会做更多的工作并且没有更多的功能.
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.
这篇关于如何从 eav_attribute 表中获取实体(例如客户)的数据以显示在管理员的客户网格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!