Post by daledude » Thu Feb 28, 2013 4:40 am

OpenCart v: 1.4.9.2 (yes, old... we're upgrading soon)

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; 
}
}
7) Replace YOUR_LIST with the ID of your list. To get the Id, go MailChimp -> List -> Settings.
8) 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 :)
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.

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'] = '';
		}
and I also tried replacing that code entirely with a MailChimp mixup hack that I have no idea is right or not:

Code: Select all

// Begin MailChimp
						
		if (isset($this->request->post['newsletter'])) {
				$this->data['newsletter'] = mailChimpSubscribe($data);
			} else {
				$this->data['newsletter'] = '';
			}
			
	// End MailChimp
Anybody have any suggestions? Thanks~!

New member

Posts

Joined
Thu Dec 17, 2009 1:30 am

Post by Tcalp » Fri Mar 01, 2013 12:08 am

I don't know too much about the mailchimp api, but this module may save you some time / headache : http://www.opencart.com/index.php?route ... on_id=1707

As for the functions you pasted, I assume you have entered the required data such as your API KEY / List Name / ETc ?

Increase Page Speed (#1 rated commercial extension on OpenCart Marketplace)
15in1 Essential Extensions Value Pack Premium Customer Testimonials Reward Points Extended Admin Security Lockdown Suite

Image
irc.freenode.net #opencart


User avatar
Active Member

Posts

Joined
Wed Jul 06, 2011 1:49 pm

Post by daledude » Fri Mar 01, 2013 1:45 am

I know that it's an easy deal, but I haven't learned enough PHP yet to understand how to port it over from the newsletter.php to the create.php.

I did go ahead and buy that extension, because I'm sick of fooling with it. :P Having to part with that $30 just motivated me to buy a PHP book.

The extension works great. Though if someone knows how to do what I originally requested and could post the solution here for learning purposes, I'd love to know what I was doing wrong.

New member

Posts

Joined
Thu Dec 17, 2009 1:30 am

Post by sunster » Fri Apr 05, 2013 11:35 pm

I've been trying to get this to work also. Having this work on registration is key.

New member

Posts

Joined
Thu Sep 27, 2012 3:25 am

Post by byens » Sun Apr 07, 2013 8:18 am

Hei i found you here.

I will be glad if you share something about register.php i cannot find tutorial here.

Selling Kristik - Jasa Foto Aura - Kapas Vapor - supplier baju anak -


Active Member

Posts

Joined
Sat Dec 11, 2010 12:29 pm
Location - Surabaya
Who is online

Users browsing this forum: No registered users and 167 guests