Add (billing) company name to customer collection
-
13-12-2019 - |
Question
I want to add the billing company name to the customer grid. I found this answer. To help me with the grid. But I can't seem to get the company name to appear.
Can anybody help me with this?
The observed event is eav_collection_abstract_load_before
. The observer method:
public function eavCollectionAbstractLoadBefore(Varien_Event_Observer $observer)
{
$collection = $observer->getCollection();
if (!isset($collection) || !$collection instanceof Mage_Customer_Model_Resource_Customer_Collection) return;
/* @var $collection Mage_Customer_Model_Resource_Customer_Collection */
Mage::log( (string) $collection->getSelect() );
// Add billing company here
// $collection->addAttributeToSelect('billing_company');
}
The SQL the Mage:log gives:
SELECT
`e`.*,
`at_prefix`.`value` AS `prefix`,
`at_firstname`.`value` AS `firstname`,
`at_middlename`.`value` AS `middlename`,
`at_lastname`.`value` AS `lastname`,
`at_suffix`.`value` AS `suffix`,
CONCAT(IF(at_prefix.value IS NOT NULL AND at_prefix.value != '', CONCAT(LTRIM(RTRIM(at_prefix.value)), ' '), ''),
LTRIM(RTRIM(at_firstname.value)), ' ', IF(at_middlename.value IS NOT NULL AND at_middlename.value != '',
CONCAT(LTRIM(RTRIM(at_middlename.value)), ' '), ''),
LTRIM(RTRIM(at_lastname.value)),
IF(at_suffix.value IS NOT NULL AND at_suffix.value != '', CONCAT(' ', LTRIM(RTRIM(at_suffix.value))),
'')) AS `name`,
`at_default_billing`.`value` AS `default_billing`,
`at_billing_postcode`.`value` AS `billing_postcode`,
`at_billing_city`.`value` AS `billing_city`,
`at_billing_telephone`.`value` AS `billing_telephone`,
`at_billing_region`.`value` AS `billing_region`,
`at_billing_country_id`.`value` AS `billing_country_id`
FROM `iop_customer_entity` AS `e` LEFT JOIN `iop_customer_entity_varchar` AS `at_prefix`
ON (`at_prefix`.`entity_id` = `e`.`entity_id`) AND (`at_prefix`.`attribute_id` = '4')
LEFT JOIN `iop_customer_entity_varchar` AS `at_firstname`
ON (`at_firstname`.`entity_id` = `e`.`entity_id`) AND (`at_firstname`.`attribute_id` = '5')
LEFT JOIN `iop_customer_entity_varchar` AS `at_middlename`
ON (`at_middlename`.`entity_id` = `e`.`entity_id`) AND (`at_middlename`.`attribute_id` = '6')
LEFT JOIN `iop_customer_entity_varchar` AS `at_lastname`
ON (`at_lastname`.`entity_id` = `e`.`entity_id`) AND (`at_lastname`.`attribute_id` = '7')
LEFT JOIN `iop_customer_entity_varchar` AS `at_suffix`
ON (`at_suffix`.`entity_id` = `e`.`entity_id`) AND (`at_suffix`.`attribute_id` = '8')
LEFT JOIN `iop_customer_entity_int` AS `at_default_billing`
ON (`at_default_billing`.`entity_id` = `e`.`entity_id`) AND (`at_default_billing`.`attribute_id` = '13')
LEFT JOIN `iop_customer_address_entity_varchar` AS `at_billing_postcode`
ON (`at_billing_postcode`.`entity_id` = `at_default_billing`.`value`) AND
(`at_billing_postcode`.`attribute_id` = '30')
LEFT JOIN `iop_customer_address_entity_varchar` AS `at_billing_city`
ON (`at_billing_city`.`entity_id` = `at_default_billing`.`value`) AND (`at_billing_city`.`attribute_id` = '26')
LEFT JOIN `iop_customer_address_entity_varchar` AS `at_billing_telephone`
ON (`at_billing_telephone`.`entity_id` = `at_default_billing`.`value`) AND
(`at_billing_telephone`.`attribute_id` = '31')
LEFT JOIN `iop_customer_address_entity_varchar` AS `at_billing_region`
ON (`at_billing_region`.`entity_id` = `at_default_billing`.`value`) AND (`at_billing_region`.`attribute_id` = '28')
LEFT JOIN `iop_customer_address_entity_varchar` AS `at_billing_country_id`
ON (`at_billing_country_id`.`entity_id` = `at_default_billing`.`value`) AND
(`at_billing_country_id`.`attribute_id` = '27')
WHERE (`e`.`entity_type_id` = '1')
For completion sake, the add column to grid oberver core_block_abstract_to_html_before
with method:
public function coreBlockAbstractToHtmlBefore(Varien_Event_Observer $observer)
{
$grid = $observer->getBlock();
if (!$grid instanceof Mage_Adminhtml_Block_Customer_Grid) return;
/** @var Mage_Adminhtml_Block_Customer_Grid $grid */
$grid->addColumnAfter(
'billing_company',
array(
'header' => 'company_name', //@todo translate
'index' => 'billing_company',
),
'name'
);
}
Solution
Try this
config.xml
<adminhtml>
<events>
<core_block_abstract_to_html_before>
<observers>
<{observer_name}>
<class>{namespace}_{module}/observer</class>
<method>beforeBlockToHtml</method>
</{observer_name}>
</observers>
</core_block_abstract_to_html_before>
<eav_collection_abstract_load_before>
<observers>
<{observer_name}>
<class>{namespace}_{module}/observer</class>
<method>beforeCollectionLoad</method>
</{observer_name}>
</observers>
</eav_collection_abstract_load_before>
</events>
</adminhtml>
observer.php
<?php
class Namespace_ModuleName_Model_Observer
{
public function beforeBlockToHtml(Varien_Event_Observer $observer)
{
$grid = $observer->getBlock();
/**
* Mage_Adminhtml_Block_Customer_Grid
*/
if ($grid instanceof Mage_Adminhtml_Block_Customer_Grid) {
$grid->addColumnAfter(
'billing_company',
array(
'header' => Mage::helper('customer')->__('Billing Company'),
'index' => 'billing_company'
),
'email'
);
}
}
public function beforeCollectionLoad(Varien_Event_Observer $observer)
{
$collection = $observer->getCollection();
if (!isset($collection)) {
return;
}
/**
* Mage_Customer_Model_Resource_Customer_Collection
*/
if ($collection instanceof Mage_Customer_Model_Resource_Customer_Collection) {
/* @var $collection Mage_Customer_Model_Resource_Customer_Collection */
$collection->joinAttribute('billing_company', 'customer_address/company', 'default_billing', null, 'left');
}
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange