This repository has been archived by the owner on May 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.php
211 lines (159 loc) · 5.52 KB
/
Main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
class Ebizmarts_MailChimp_Main extends BClass {
public $configPath = 'modules/Ebizmarts_MailChimp/';
public $moduleActive = false;
public $mc;
public function __construct() {
$this->moduleActive = (bool)BConfig::i()->get($this->configPath . 'active');
if($this->moduleActive) {
include_once __DIR__ . '/lib/mailchimp.php';
$apiKey = BConfig::i()->get('modules/Ebizmarts_MailChimp/api_key');
if($apiKey) {
$this->mc = new Mailchimp($apiKey);
}
}
}
/**
* Handle emailaftersave event.
*
* @param $args
* @return Ebizmarts_MailChimp_Main
*/
public function onEmailAfterSave($args) {
if(!$this->moduleActive) {
return $this;
}
$pref = $args['model'];
$sub = (int)$pref->get('sub_newsletter');
$unsub = (int)$pref->get('unsub_all');
$defaultListId = BConfig::i()->get($this->configPath . 'list_id');
$postData = array(
'id' => $defaultListId,
'email' => array(
'email' => $pref->get('email'),
)
);
//Subscribe email if record is new and subscribe
if($pref->isNewRecord()) {
//Check if subscribe flag is ON
if(1 === $sub) {
//Frontend, get customer data if logged in.
$cust = FCom_Customer_Model_Customer::i()->sessionUser();
if(false !== $cust) {
$postData['merge_vars'] = array(
'FNAME' => $cust->get('firstname'),
'LNAME' => $cust->get('lastname'),
);
}
try {
$this->mc->call('lists/subscribe', $postData);
}catch(Exception $ex) {
BDebug::logException($ex);
}
//@TODO: Double opt-in handle in Sellvana?
}
}
else {
$_postData = $postData;
$_postData['emails'] = array(
array('email' => $postData['email']['email']),
);
unset($_postData['email']);
$memberInfo = $this->mc->call('lists/member-info', $_postData);
$isSubscribed = false;
if(is_array($memberInfo) and !empty($memberInfo)) {
if((int)$memberInfo['success_count'] === 1) {
$isSubscribed = true;
}
}
if(1 === $unsub) {
//Unsubscribe email
if($isSubscribed) {
$this->mc->call('lists/unsubscribe', $postData);
}
}
else {
if(1 === $sub) {
//Subscribe email if not subscribed
if(!$isSubscribed) {
$this->mc->call('lists/subscribe', $postData);
}
}
}
}
return $this;
}
/**
* Handle subscriptor deletion.
*
* @param $args
* @return Ebizmarts_MailChimp_Main
*/
public function onEmailAfterDelete($args) {
if(!$this->moduleActive) {
return $this;
}
$pref = $args['model'];
//Get default list ID from config.
$defaultListId = BConfig::i()->get($this->configPath . 'list_id');
//Unsubscribe /*/Delete*/
$this->mc->call('lists/unsubscribe', array(
'id' => $defaultListId,
'email' => array(
'email' => $pref->get('email'),
),
));
return $this;
}
/**
* Push frontend order to ecommerce360.
*
* @param $args
* @return Ebizmarts_MailChimp_Main
*/
public function onCreateFromCartAfter($args) {
$ecommEnabled = (bool)BConfig::i()->get($this->configPath . 'ecomm');
if(!$this->moduleActive or !$ecommEnabled) {
return $this;
}
$order = $args['order'];
$date = explode(' ', $order->get('create_at'));
$ecommOrder = array(
'id' => $order->get('unique_id'),
'email' => $order->get('customer_email'),
'total' => $order->get('grandtotal'),
'order_date' => $date[0],
'store_id' => 1,
'store_name' => BConfig::i()->get('modules/FCom_Core/site_title'),
'items' => array(),
);
$shippingAmount = $order->get('shipping_amount');
if($shippingAmount) {
$ecommOrder['shipping'] = $shippingAmount;
}
$taxAmount = $order->get('tax');
if($taxAmount) {
$ecommOrder['tax'] = $shippingAmount;
}
$items = $order->items();
foreach($items as $item) {
$product = json_decode($item->get('product_info'));
$_item = array(
'product_id' => (int)$product->id,
'sku' => substr($product->local_sku, 0, 30),
'product_name' => $product->product_name,
'category_id' => 0,
'category_name' => 'None',
'qty' => $item->get('qty'),
'cost' => $product->cost,
);
$ecommOrder['items'][] = $_item;
}
try {
$ecommResult = $this->mc->call('ecomm/order-add', array('order' => $ecommOrder));
}catch(Exception $ex) {
BDebug::logException($ex);
}
return $this;
}
}