How to add guests to newsletter subscription in Magento
Posted on: January 11, 2015

Sometimes there is a need to sign customers to Newsletter. We consider an option when Newsletter was not on the site. We created and launched newsletter. Where to get followers? Existing customers we can sign from the admin end. But we can not sign those not registered at purchase. How sign Guests to Newsletter? Let me offer you my version.
The information will be relevant for the next situation:

1. Guests can buy
2. Newsletters module is switched off
3. You must send a letter immediately to all new subscribers(need to execute all items)

Note: Items marked with a * must be performed only if should send message to new subscribers.


*When substsribe or unsubscribe to newsletters magento sends a message to user. If you already have an active subscribers, you should disable this functionality to not confuse them. After all the manipulations newsletter will include all temporarily disabled. How to disable sending emails? You can use the app “Disable Email Notifications” from MageryThemes developer. Install the application, disable sending of mails and move to the next step.


Take all guests email from sales_flat_order table. These are emails guests, we will be adding to the mailing list. Temporarily list can be saved to a text file.

SELECT `customer_email` FROM `sales_flat_order` WHERE ISNULL(`customer_id`) GROUP BY `customer_email`;

For a list of already signed users use request:

SELECT * FROM `newsletter_subscriber`;

Find out which emails are already subscribed to eliminate them and not to sign twice. For example:

$newSubscribers = array('user1@example.com', 'user2@example.com', 'user3@example.com');
$existingSubscribers = array('user11@example.com', 'user2@example.com');
$newSubscribers = array_diff($newSubscribers, $existingSubscribers);

So we get an array $newSubscribers new emails.


Prepare request to insert new followers. In magento each subscriber may unsubscribe from the mailing list. Each has a unique code.
Generate a code for each. For this we use the mechanism described below:

function randomSequence($length = 32)
{
    $id = '';
    $par = array();
    $char = array_merge(range('a', 'z'), range(0, 9));
    $charLen = count($char) - 1;
    for ($i = 0; $i < $length; $i++) {
        $disc = mt_rand(0, $charLen);
        $par[$i] = $char[$disc];
        $id = $id . $char[$disc];
    }
    return $id;
}
$usersQty = count($newSubscribers);
for ($i = 0; $i < $usersQty; $i++) {
    echo randomSequence() . '<br>';
}

*Select all unsubscribed users for now. Save the resulting list id. At the end of their need to unsubscribe ago

SELECT * FROM `newsletter_subscriber` WHERE `subscriber_status` = 3;

*Unsubscribe all existing emails:

UPDATE `newsletter_subscriber` SET `subscriber_status` = '3';

Prepare queries to insert new emails:

INSERT INTO `newsletter_subscriber` (`subscriber_id`, `store_id`, `change_status_at`, `customer_id`, `subscriber_email`, `subscriber_status`, `subscriber_confirm_code`) VALUES (NULL, '1', NULL, '0', 'user1@example.com', '1', 'ckzrp1mv97agou58aj5i6ehg4ugwrcgt');

And thus insert queries for all new subscribers.


*Sends all messages (run Newsletter)


 

*Signs of subscribers (after mailing):

UPDATE `newsletter_subscriber` SET `subscriber_status` = '1';


*Unsubscribe those who was unsubscribe:

UPDATE `newsletter_subscriber` SET `subscriber_status` = '3' WHERE `subscriber_id` = 35; //substitute necessary ID


Thus, the list of our newsletter will all our customers, both registered and guests. Of course all the users are able to unsubscribe from the mailing list.

 

Leave a Reply

Your email address will not be published. Required fields are marked *