Magento – Getting customer collection with only those that are subscribed to newsletters

customerimportnewsletter

I need to import a load of customers to a third party email company but need to only import those that are currently subscribed to newsletters. The import itself has to be a file of customer details which I need to generate. I know I could load up all customers into a collection and iterate through them, only inserting those that are subscribed into the import file but as we have 90000 plus customers I would rather just load the ones that are subscribed into the collection. The import itself uses an API but it needs to have the import file added as an attachment (fine doing that, just including it for information).

Any ideas?

Best Answer

You have to join EAV collection with regular table. It is achievable with joinTable method of Mage_Eav_Model_Entity_Collection_Abstract class. The forth parameter defines the WHERE condition because apparently you can't use addAttributeToFilter method here.

$collection = Mage::getModel('customer/customer')->getCollection();
$collection->addNameToSelect()
    ->joinTable('newsletter_subscriber', 'customer_id=entity_id', array('subscriber_status'), '{{table}}.subscriber_status=1');

Please let me know if something is unclear.

Related Topic