1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
class EcommerceMailchimpSignupMemberExtension extends DataExtension |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
private static $db = array( |
|
|
|
|
8
|
|
|
'SignedUpToMailchimp' => "Boolean" |
9
|
|
|
); |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Store the user in MailChimp |
13
|
|
|
* @param array $mergeVars |
14
|
|
|
* @return Boolean |
15
|
|
|
*/ |
16
|
|
|
public function subscribeToMailchimp($mergeVars = array()) |
17
|
|
|
{ |
18
|
|
|
$listID = Config::inst()->get('EcommerceMailchimpSignupDecoratorFormFixes', 'mailchimp_list_id'); |
19
|
|
|
|
20
|
|
|
$mailChimp = $this->getMailChimpAPI(); |
21
|
|
|
|
22
|
|
|
$result = $mailChimp->post( |
|
|
|
|
23
|
|
|
"lists/".$listID."/members", |
24
|
|
|
array( |
25
|
|
|
'status' => 'subscribed', |
26
|
|
|
'email_address' => $this->owner->Email, |
27
|
|
|
"merge_fields" => $mergeVars + array( |
28
|
|
|
"FNAME" => $this->owner->FirstName, |
29
|
|
|
"LNAME" => $this->owner->Surname |
30
|
|
|
) |
31
|
|
|
) |
32
|
|
|
); |
33
|
|
View Code Duplication |
if ($mailChimp->success()) { |
|
|
|
|
34
|
|
|
$this->owner->SignedUpToMailchimp = true; |
35
|
|
|
$this->owner->write(); |
36
|
|
|
return true; |
37
|
|
|
} else { |
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
return $result; |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function existsOnMailchimp() |
48
|
|
|
{ |
49
|
|
|
if ($this->owner->SignedUpToMailchimp) { |
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$mailChimp = $this->getMailChimpAPI(); |
54
|
|
|
|
55
|
|
|
$listID = Config::inst()->get('EcommerceMailchimpSignupDecoratorFormFixes', 'mailchimp_list_id'); |
56
|
|
|
|
57
|
|
|
$subscriberHash = $mailChimp->subscriberHash($this->owner->Email); |
58
|
|
|
|
59
|
|
|
$result = $mailChimp->get( |
|
|
|
|
60
|
|
|
"lists/".$listID."/members/".$subscriberHash |
61
|
|
|
); |
62
|
|
View Code Duplication |
if ($mailChimp->success()) { |
|
|
|
|
63
|
|
|
$this->owner->SignedUpToMailchimp = true; |
64
|
|
|
$this->owner->write(); |
65
|
|
|
return true; |
66
|
|
|
} else { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function updateOnMailchimp() |
76
|
|
|
{ |
77
|
|
|
$mailChimp = $this->getMailChimpAPI(); |
78
|
|
|
|
79
|
|
|
$listID = Config::inst()->get('EcommerceMailchimpSignupDecoratorFormFixes', 'mailchimp_list_id'); |
80
|
|
|
|
81
|
|
|
$subscriberHash = $mailChimp->subscriberHash($this->owner->Email); |
82
|
|
|
|
83
|
|
|
$result = $mailChimp->patch( |
|
|
|
|
84
|
|
|
"lists/".$listID."/members/".$subscriberHash, |
85
|
|
|
array( |
86
|
|
|
'merge_fields' => array( |
87
|
|
|
'FNAME'=> $this->owner->FirstName, |
88
|
|
|
'LNAME'=>$this->owner->Surname |
89
|
|
|
) |
90
|
|
|
) |
91
|
|
|
); |
92
|
|
|
if ($mailChimp->success()) { |
|
|
|
|
93
|
|
|
return true; |
94
|
|
|
} else { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* casted variable |
101
|
|
|
*/ |
102
|
|
|
private static $_mailchimp_api = null; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return MailChimp |
106
|
|
|
*/ |
107
|
|
|
protected function getMailChimpAPI() |
108
|
|
|
{ |
109
|
|
|
require_once(Director::baseFolder().'/vendor/drewm/mailchimp-api/src/MailChimp.php'); |
110
|
|
|
if (self::$_mailchimp_api) { |
|
|
|
|
111
|
|
|
//.. |
112
|
|
|
} else { |
113
|
|
|
self::$_mailchimp_api = new \DrewM\MailChimp\MailChimp(Config::inst()->get('EcommerceMailchimpSignupDecoratorFormFixes', 'mailchimp_api_key')); |
114
|
|
|
} |
115
|
|
|
return self::$_mailchimp_api; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.