将所有地址详细信息添加到客户收集中

时间:2012-10-15 13:47:03

标签: magento collections

我希望将所有客户地址属性加入到完整的客户集合中。

$collection = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*');
$collection->joinField('is_active', 'customer/address', 'is_active','parent_id=entity_id');

给了我致命的错误:

Mage_Core_Exception: Can't retrieve entity config: customer/address in /app/Mage.php on line 563

由于我使用的是Enterprise,因此我们可以添加客户地址属性,因此使用加入特定地址属性的方法并不像在此代码段中那样令人满意:

$collection->addNameToSelect()
    ->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')

有什么想法吗?谢谢。

1 个答案:

答案 0 :(得分:2)

我设法找到了一种方法,虽然它不是超级优雅但它对我有用。基本上,我们可以遍历所有地址属性并以这种方式添加joinAttribute()

$addressCollection = Mage::getModel('customer/address')->getCollection()->addAttributeToSelect('*');
$addressCollection->getSelect()->limit(1);
$address = $addressCollection->getFirstItem();
foreach ($address->getAttributes() as $attribute) {
    if (is_null($attribute->getAttributeID()) || is_null($attribute->getFrontendLabel()) || ($attribute->getFrontendLabel() == '')) {
        continue;
    }
    $collection->joinAttribute($attribute->getName(), 'customer_address/' . $attribute->getName(), 'default_billing', null, 'left');
}

请注意,我们忽略没有标签的属性,因为我只想要“真实的”客户地址信息。

相关问题