I'm using this free OpenCart MailChimp extension: http://www.opencart.com/index.php?route ... S5qfeugmCQ It works beautifully if you are subscribing to the newsletter by going to your profile and selecting Yes to newsletter if you originally selected No when creating your account, but it doesn't work at all when initially setting up the account. You only add this dev's code into catalog/controller/account/newsletter.php, when I am sure that similar code needs to be added to catalog/controller/account/create.php in order for it to work at initial account setup when you click Yes to subscribe there.
Since the extension's free, he offers no support, and the code's plainly pasted into the Documentation tab, I assume he won't care I post the code here.
This is what's pasted into newsletter.php, and would need to be adapted for create.php:
1) BACKUP YOUR SERVER. I am not responsible for anything you do to your server. Back it up before you change a thing.
2) Download http://apidocs.mailchimp.com/api/downlo ... -class.zip to your pc.
3) Extract "MCAPI.class.php" from it and place it in your OpenCart "system/library" folder and upload to the server.
4) Open your OpenCart "catalog/controller/account/newsletter.php" file and create a new line AFTER "$this->session->data['success'] = $this->language->get('text_success');" on Line 19 or there about, I'm using an older, modified version, excuse mistakes with line numbers. On this new line, add the following text:
Code: Select all
$data = array(
'email' => $this->customer->getEmail(),
'firstname' => $this->customer->getFirstname(),
'lastname' => $this->customer->getLastname()
);
if($this->request->post['newsletter']) {
$this->mailChimpSubscribe($data);
} else {
$this->mailChimpUnsubscribe($data);
}
5) save the file, and goto the last "}" in the file, and add a blank line above it.
6) Paste in the following two functions:
Code: Select all
function mailChimpSubscribe($data){
if(!$data['email']){ return "No email address provided"; }
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $data['email'])) {
return "Email address is invalid";
}
require_once('/var/www/system/library/MCAPI.class.php');
$api = new MCAPI('YOUR_API_KEY');
$mergeVars = array('FNAME'=>$data['firstname'],
'LNAME'=>$data['lastname']
);
if($api->listSubscribe("YOUR_LIST", $data['email'], $mergeVars) === true) {
return 'Success! Check your email to confirm sign up.';
} else { return 'Error: ' . $api->errorMessage; }
}
Code: Select all
function mailChimpUnsubscribe($data) {
if(!$data['email']){ return "No email address provided"; }
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $data['email'])) {
return "Email address is invalid";
}
require_once('/var/www/system/library/MCAPI.class.php');
$api = new MCAPI('YOUR_API_KEY');
if($api->listUnsubscribe("YOUR_LIST", $data['email'])){
return 'Success! Check your email to confirm sign up.';
} else {
return 'Error: ' . $api->errorMessage;
}
}
As I've said, it works beautifully if you're signing up from your profile, but not at all from the initial account register page. I was thinking that mimicking the steps on create.php would work, so I copied/pasted the two subscribe/unsubscribe functions to the bottom of create.php, and also added the data array part toward the top of the create.php. The registration form submits successfully with no errors, the user is created in OpenCart, but the data doesn't seem to get sent to MailChimp.7) Replace YOUR_LIST with the ID of your list. To get the Id, go MailChimp -> List -> Settings.
Replace YOUR_API_KEY with your API key from MailChimp.
9) Save and upload the file to your host.
10) Test, and thank me if it works
I put the data array part after
Code: Select all
if (isset($this->request->post['newsletter'])) {
$this->data['newsletter'] = $this->request->post['newsletter'];
} else {
$this->data['newsletter'] = '';
}
Code: Select all
// Begin MailChimp
if (isset($this->request->post['newsletter'])) {
$this->data['newsletter'] = mailChimpSubscribe($data);
} else {
$this->data['newsletter'] = '';
}
// End MailChimp