Ashutosh
Ashutosh

Reputation: 4675

Magento get all customers who are not unsubscribed

I'm trying to get a list of all customers who are not unsubscribed. That is I don't want those customers who have unsubscribed themselves after subscribing. I can get list of customer like this:

$collection = Mage::getModel('customer/customer')->getCollection()
->addAttributeToSelect('firstname')
->addAttributeToSelect('lastname')
->addAttributeToSelect('email');

What should I apply on this $collection to filter out unsubscribed customers.

Thanks in advance

Upvotes: 0

Views: 1133

Answers (2)

Ashutosh
Ashutosh

Reputation: 4675

Finally I did this:

$array = array();

$collectionSub = Mage::getResourceModel ('newsletter/subscriber_collection')           
        -> AddFieldToFilter ('subscriber_status', array ('eq' => 3));  // unsubscribed


foreach($collectionSub as $sub) {
    $array[] = $sub['subscriber_email'];
}

$collection = Mage::getModel('customer/customer')->getCollection()
    ->addAttributeToSelect('firstname')
    ->addAttributeToSelect('lastname')
    ->addAttributeToSelect('email')
    ->addFieldToFilter('email', array('nin', $array));     // all others that have not unsubscribed

Upvotes: 0

Knase
Knase

Reputation: 1274

Try this code:

$collectionSub = Mage::getModel('newsletter/subscriber')->getCollection()
->addAttributeToSelect('email')
->addFieldToFilter('subscriber_status'.  array('eq' => 0)) // 0 not subscribed
->load();
$array = array();
foreach($collectionSub as $sub) {
 $array[] = $sub['subscriber_email'];
}
$collection = Mage::getModel('customer/customer')->getCollection()
->addAttributeToSelect('firstname')
->addAttributeToSelect('lastname')
->addAttributeToSelect('email')
->addFieldToFilter('email', array('in', $array);

Read Magento Wiki.

Upvotes: 1

Related Questions