我现在要给导出的.CSV文件添加Subscribe项,需要修改传给_prepareDownloadResponse()方法的第2个参数$model->export():
具体步骤:首先修改$collection,让$collection包含customers的subscriber数据
1 $collection->getSelect()->joinLeft(
2 array('ns' => 'newsletter_subscriber'),
3 'ns.customer_id = e.entity_id',
4 'ns.subscriber_status'
5 );
然后在$row数组中添加Subscriber数据:
按 Ctrl+C 复制代码
class: Mage_ImportExport_Model_Export_Entity_Customer
/**
* Export process.
*
* @return string
*/
public function export()
{
$collection = $this->_prepareEntityCollection(Mage::getResourceModel('customer/customer_collection'));
$validAttrCodes = $this->_getExportAttrCodes();
$writer = $this->getWriter();
$defaultAddrMap = Mage_ImportExport_Model_Import_Entity_Customer_Address::getDefaultAddressAttrMapping();
// prepare address data
$addrAttributes = array();
$addrColNames = array();
$customerAddrs = array();
foreach (Mage::getResourceModel('customer/address_attribute_collection')
->addSystemHiddenFilter()
->addExcludeHiddenFrontendFilter() as $attribute) {
$options = array();
$attrCode = $attribute->getAttributeCode();
if ($attribute->usesSource() && 'country_id' != $attrCode) {
foreach ($attribute->getSource()->getAllOptions(false) as $option) {
foreach (is_array($option['value']) ? $option['value'] : array($option) as $innerOption) {
if (strlen($innerOption['value'])) { // skip ' -- Please Select -- ' option
$options[$innerOption['value']] = $innerOption['label'];
}
}
}
}
$addrAttributes[$attrCode] = $options;
$addrColNames[] = Mage_ImportExport_Model_Import_Entity_Customer_Address::getColNameForAttrCode($attrCode);
}
foreach (Mage::getResourceModel('customer/address_collection')->addAttributeToSelect('*') as $address) {
$addrRow = array();
foreach ($addrAttributes as $attrCode => $attrValues) {
if (null !== $address->getData($attrCode)) {
$value = $address->getData($attrCode);
if ($attrValues) {
$value = $attrValues[$value];
}
$column = Mage_ImportExport_Model_Import_Entity_Customer_Address::getColNameForAttrCode($attrCode);
$addrRow[$column] = $value;
}
}
$customerAddrs[$address['parent_id']][$address->getId()] = $addrRow;
}
// create export file
$writer->setHeaderCols(array_merge(
$this->_permanentAttributes, $validAttrCodes,
array('password'), $addrColNames,
array_keys($defaultAddrMap),
array('subscribe_status')
));
$collection->getSelect()->joinLeft(
array('ns' => 'newsletter_subscriber'),
'ns.customer_id = e.entity_id',
'ns.subscriber_status'
);
//Mage::log((string)$collection->getSelect(),7,'export.log');
foreach ($collection as $itemId => $item) { // go through all customers
$row = array();
// go through all valid attribute codes
foreach ($validAttrCodes as $attrCode) {
$attrValue = $item->getData($attrCode);
if (isset($this->_attributeValues[$attrCode])
&& isset($this->_attributeValues[$attrCode][$attrValue])
) {
$attrValue = $this->_attributeValues[$attrCode][$attrValue];
}
if (null !== $attrValue) {
$row[$attrCode] = $attrValue;
}
}
$row[self::COL_WEBSITE] = $this->_websiteIdToCode[$item['website_id']];
$row[self::COL_STORE] = $this->_storeIdToCode[$item['store_id']];
$row['subscribe_status'] = $this->getSubscriberLabel($item);
//Mage::log($row,7,'export.log');
// addresses injection
$defaultAddrs = array();
foreach ($defaultAddrMap as $colName => $addrAttrCode) {
if (!empty($item[$addrAttrCode])) {
$defaultAddrs[$item[$addrAttrCode]][] = $colName;
}
}
if (isset($customerAddrs[$itemId])) {
while (($addrRow = each($customerAddrs[$itemId]))) {
if (isset($defaultAddrs[$addrRow['key']])) {
foreach ($defaultAddrs[$addrRow['key']] as $colName) {
$row[$colName] = 1;
}
}
$writer->writeRow(array_merge($row, $addrRow['value']));
$row = array();
}
} else {
$writer->writeRow($row);
}
}
return $writer->getContents();
}
按 Ctrl+C 复制代码
|