UPDATED: Magento add customer attribute filter to order grid
I have extended the Mage_Adminhtml_Block_Sales_Order_Grid
class with a custom module to add several customer attributes (Magento EE 1.10) to the grid.
Two of the attributes I added are text fields (i.e. they live in the customer_entity_varchar
table, and I was able to add them to the collection and display them in the grid. So far so good.
A third attribute is a select, so the values live in the customer_entity_int
, the eav_attribute_option
and the eav_attribute_option_value
tables. I added the necessary values to the collection (using $collection->getSelect()->joinLeft(.....)
. Again, so far so good.
My problem is being able to display and filter the attribute at the same time.
Inside the _prepareColumns()
function in my MyCompany_MyModule_Block_Adminhtml_Order_Grid
class, if I add a column like this, - as expected - I can display the values of the attribute on each row, but I don't get a drop down filter in the header:
protected function _prepareColumns()
{
...
$this->addColumn('bureau', array(
'header' => Mage::helper('sales')->__('Bureau'),
'index' => 'bureau',
'type' => 'text'
));
...
}
Following the example of status
, and adding the column like this, gives me the drop down filter in the header, but it no longer displays the values for the attribute in each row:
protected function _prepareColumns()
{
...
$this->addColumn('bureau', array(
'header' => Mage::helper('sales')->__('Bureau'),
'index' => 'bureau',
'type' => 'options',
'options' => $this->_getBureauOptions(),
'filter_index' => 'value_option_table.option_id'
));
...
}
protected function _getBureauOptions()
{
$bureau = Mage::getResourceModel('eav/entity_attribute_collection')
->setCodeFilter('bureau')
->getFirstItem();
$bureauOptions = $bureau->getSource()->getAllOptions(false);
$optionsArr = array();
foreach (开发者_开发百科$bureauOptions as $option) {
$optionsArr[$option['value']] = $option['label'];
}
return $optionsArr;
}
Any advice / explanation would be much appreciated.
UPDATE:
It turns out that my code also causes a SQL error in a multi-website environment when an admin user only has permissions for some websites:
"SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'store_id' in where clause is ambiguous"
@clockworkgeek had the answer to the first part of my question.
The problem was that my joinLeft()
was retrieving text values from the attribute options, while I should have been retrieving integer values when using 'type => 'options'
.
Once I changed my joinLeft()
to only retrieve integer values from customer_entity_int
(actually a simpler join), the filtering and display worked flawlessly - thank you sir.
I will re-post my second issue (about SQL errors caused by permissions) as a separate question.
精彩评论