cartsguru - Version 1.1.3

Version Notes

- Refactor address management
- Remove additionnal fields
- Add totalATI in sent data

Download this release

Release Info

Developer Maxime Pruvost
Extension cartsguru
Version 1.1.3
Comparing to
See all releases


Code changes from version 1.1.2 to 1.1.3

app/code/local/Cartsguru/Model/Observer.php CHANGED
@@ -24,7 +24,8 @@ class Cartsguru_Model_Observer
24
  */
25
  public function orderSaveAfter($observer)
26
  {
27
- Mage::getModel('cartsguru/webservice')->sendOrder($observer->getOrder());
 
28
  }
29
 
30
  /**
@@ -33,19 +34,6 @@ class Cartsguru_Model_Observer
33
  */
34
  public function quoteSaveBefore($observer)
35
  {
36
- $quote = $observer->getQuote();
37
- $request = Mage::app()->getRequest()->getParams();
38
- $cache = Mage::app()->getCache();
39
-
40
- if (isset($request['billing'])) {
41
- if (isset($request['billing']['telephone'])) {
42
- $quote->setTelephone($request['billing']['telephone']);
43
- }
44
-
45
- if (isset($request['billing']['country_id'])) {
46
- $quote->setData('country', $request['billing']['country_id']);
47
- }
48
- }
49
  }
50
 
51
  /**
@@ -54,34 +42,17 @@ class Cartsguru_Model_Observer
54
  */
55
  public function customerSaveAfter($observer)
56
  {
57
- Mage::getModel('cartsguru/webservice')->sendAccount($observer->getCustomer());
 
58
  }
59
 
 
 
 
 
60
  public function productAddAfter($observer)
61
  {
62
  $quote = Mage::getModel('checkout/session')->getQuote();
63
- $customer = $quote->getCustomer();
64
- if (isset($customer)) {
65
- $billingAddress = $customer->getDefaultBillingAddress();
66
- if ($billingAddress) {
67
- if (empty($quote->getTelephone())) {
68
- $quote->setTelephone($billingAddress->getTelephone());
69
- }
70
- if (empty($quote->getCountry())) {
71
- $quote->setData('country', $billingAddress->getCountryId());
72
- }
73
- }
74
- }
75
-
76
  Mage::getModel('cartsguru/webservice')->sendAbadonnedCart($quote);
77
  }
78
-
79
- /**
80
- * This method set telephone in session
81
- */
82
- public function setTelephoneInSession()
83
- {
84
- $telephone = Mage::app()->getRequest()->getParams()['billing']['telephone'];
85
- Mage::getSingleton("core/session")->setData("telephone", $telephone);
86
- }
87
  }
24
  */
25
  public function orderSaveAfter($observer)
26
  {
27
+ $order = $observer->getOrder();
28
+ Mage::getModel('cartsguru/webservice')->sendOrder($order);
29
  }
30
 
31
  /**
34
  */
35
  public function quoteSaveBefore($observer)
36
  {
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  }
38
 
39
  /**
42
  */
43
  public function customerSaveAfter($observer)
44
  {
45
+ $customer = $observer->getCustomer();
46
+ Mage::getModel('cartsguru/webservice')->sendAccount($customer);
47
  }
48
 
49
+ /**
50
+ * This method - hook for quote save api call
51
+ * @param $observer
52
+ */
53
  public function productAddAfter($observer)
54
  {
55
  $quote = Mage::getModel('checkout/session')->getQuote();
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  Mage::getModel('cartsguru/webservice')->sendAbadonnedCart($quote);
57
  }
 
 
 
 
 
 
 
 
 
58
  }
app/code/local/Cartsguru/Model/Webservice.php CHANGED
@@ -7,7 +7,7 @@
7
  class Cartsguru_Model_Webservice
8
  {
9
  private $apiBaseUrl = 'https://api.carts.guru';
10
-
11
  /**
12
  * If value is empty return ''
13
  * @param $value
@@ -18,84 +18,6 @@ class Cartsguru_Model_Webservice
18
  return ($value)? $value : '';
19
  }
20
 
21
- /**
22
- * This method return order data in cartsguru format
23
- * @param $order
24
- * @return array
25
- */
26
- public function getOrderData($order)
27
- {
28
- $items = array();
29
- foreach ($order->getAllVisibleItems() as $item) {
30
- $product=$item->getProduct();
31
- $product=$product->load($product->getId());
32
- if($product->getImage() && $product->getImage() != 'no_selection') {
33
- $imageUrl=(string)Mage::helper('catalog/image')->init($item->getProduct(), 'small_image')
34
- ->constrainOnly(false)
35
- ->keepAspectRatio(true)
36
- ->keepFrame(true)
37
- ->resize(120, 120);
38
- }
39
- else {
40
- $imageUrl=$this->notEmpty(null);
41
- }
42
- $categoryNames = $this->getCatNames($item);
43
- $items[] = array(
44
- 'id' => $item->getId(), // SKU or product id
45
- 'label' => $item->getName(), // Designation
46
- 'quantity' => (int)$item->getQtyOrdered(), // Count
47
- 'totalET' => (float)$item->getPrice()*(int)$item->getQtyOrdered(), // Subtotal of item
48
- 'url' => $item->getProduct()->getProductUrl(), // URL of product sheet
49
- 'imageUrl' => $imageUrl,
50
- 'universe' => $categoryNames[1],
51
- 'category' => end($categoryNames)
52
- );
53
- }
54
-
55
- $gender = 'mister';
56
- $customer = $order->getCustomer();
57
- if ($customer && $customer->getGender()) {
58
- $gender = $customer->getGender();
59
- }
60
-
61
- $address = $order->getBillingAddress();
62
- $phone = $address->getTelephone();
63
- $country = $address->getCountryId();
64
- if (!$accountId = $order->getCustomerId()) {
65
- $accountId = $order->getCustomerEmail();
66
- }
67
-
68
- return array(
69
- 'siteId' => Mage::getStoreConfig('cartsguru/cartsguru_group/siteid', Mage::app()->getStore()), //Site Id
70
- 'id' => $order->getIncrementId(), //Order reference, the same display to the buyer
71
- 'creationDate' => $this->formatDate($order->getCreatedAt()), // Date of the order as string in json format
72
- 'cartId' => $order->getQuoteId(), // Cart identifier, source of the order
73
- 'totalET' => (float)$order->getSubtotal(), // Amount excluded taxes and excluded shipping
74
- 'state' => $this->getStatus($order->getStatus()), // waiting, confirmed, cancelled or error
75
- 'accountId' => $accountId, // Account id of the buyer
76
- 'ip' => $order->getRemoteIp(),
77
- 'civility' => $gender, // Use string in this list : 'mister','madam','miss'
78
- 'lastname' => $this->notEmpty($order->getBillingAddress()->getLastname()), // Lastname of the buyer
79
- 'firstname' => $this->notEmpty($order->getBillingAddress()->getFirstname()), // Firstname of the buyer
80
- 'email' => $this->notEmpty($order->getCustomerEmail()), // Email of the buye
81
- 'phoneNumber' => $this->notEmpty($phone), // Landline phone number of buyer (internationnal format)
82
- 'countryCode' => $this->notEmpty($country), // Country code of buyer
83
- 'items' => $items // Details info
84
- );
85
- }
86
-
87
- /**
88
- * This method send order data by api
89
- * @param $order
90
- */
91
- public function sendOrder($order)
92
- {
93
- $orderData = $this->getOrderData($order);
94
- if (!empty($orderData)) {
95
- $this->doPostRequest('/orders', $orderData);
96
- }
97
- }
98
-
99
  /**
100
  * This method format date in json format
101
  * @param $date
@@ -105,79 +27,7 @@ class Cartsguru_Model_Webservice
105
  {
106
  return date('Y-m-d\TH:i:sP', strtotime($date));
107
  }
108
-
109
- /**
110
- * This method send data on api path
111
- * @param $apiPath
112
- * @param $fields
113
- * @return Zend_Http_Response
114
- */
115
- protected function doPostRequest($apiPath, $fields)
116
- {
117
- try {
118
- $url = $this->apiBaseUrl . $apiPath;
119
- $client = new Zend_Http_Client($url);
120
- $client->setHeaders('x-auth-key', Mage::getStoreConfig('cartsguru/cartsguru_group/auth', Mage::app()->getStore()));
121
- $client->setUri($url);
122
- $client->setRawData(json_encode($fields), 'application/json');
123
- $response = $client->request(Zend_Http_Client::POST);
124
- } catch (Exception $e) {}
125
-
126
- return $response;
127
- }
128
-
129
- /**
130
- * This method map magento status to api status
131
- * @param $status
132
- * @return string
133
- */
134
- public function getStatus($status)
135
- {
136
- $status_map = array(
137
- 'processing' => 'confirmed',
138
- 'pending' => 'confirmed',
139
- 'pending_payment' => 'waiting',
140
- 'waiting_authorozation' => 'waiting',
141
- 'payment_review' => 'confirmed',
142
- 'fraud' => 'paymentFailure',
143
- 'holded' => 'error',
144
- 'complete' => 'confirmed',
145
- 'closed' => 'confirmed',
146
- 'canceled' => 'cancelled',
147
- 'pending_ogone' => 'waiting',
148
- 'processing_ogone' => 'confirmed',
149
- 'decline_ogone' => 'paymentFailure',
150
- 'cancel_ogone' => 'paymentFailure',
151
- 'pending_paypal' => 'waiting',
152
- 'paypal_canceled_reversal' => 'paymentFailure',
153
- 'paypal_reversed' => 'paymentFailure',
154
- );
155
-
156
- return isset($status_map[$status])?
157
- $status_map[$status]
158
- : $status;
159
- }
160
-
161
- /** This method return true if connect to server is ok
162
- * @return bool
163
- */
164
- public function checkAddress()
165
- {
166
- $baseUrl = Mage::getBaseUrl() . 'api/rest';
167
- $fields = array(
168
- 'plugin' => 'magento',
169
- 'pluginVersion' => '1.1.2',
170
- 'storeVersion' => Mage::getVersion()
171
- );
172
- $siteId = Mage::getStoreConfig('cartsguru/cartsguru_group/siteid', Mage::app()->getStore());
173
- $requestUrl = '/sites/' . $siteId . '/register-plugin';
174
-
175
- $response = $this->doPostRequest($requestUrl, $fields);
176
- return ($response)?
177
- ($response->getStatus() == 200)
178
- : false;
179
- }
180
-
181
  /**
182
  * Get category names
183
  * @param $item
@@ -206,17 +56,24 @@ class Cartsguru_Model_Webservice
206
 
207
  return $categoryNames;
208
  }
209
-
210
  /**
211
- * This method return abounded cart data in cartsguru api format
212
- * @param $quote
213
- * @return array|void
214
  */
215
- public function getAbadonnedCartData($quote)
216
- {
 
 
 
 
 
 
 
 
217
  $items = array();
218
- /** @var Mage_Sales_Model_Quote $quote */
219
- foreach ($quote->getAllVisibleItems() as $item) {
220
  $product=$item->getProduct();
221
  $product=$product->load($product->getId());
222
  if($product->getImage() && $product->getImage() != 'no_selection') {
@@ -230,39 +87,135 @@ class Cartsguru_Model_Webservice
230
  $imageUrl=$this->notEmpty(null);
231
  }
232
  $categoryNames = $this->getCatNames($item);
 
 
 
 
 
 
233
  $items[] = array(
234
- 'id' => (string)$item->getProduct()->getSku(), // SKU or product id
235
  'label' => $item->getName(), // Designation
236
- 'quantity' => (int)$item->getQty(), // Count
237
- 'totalET' => (float)$item->getPrice()*(int)$item->getQty(), // Subtotal of item
 
238
  'url' => $item->getProduct()->getProductUrl(), // URL of product sheet
239
  'imageUrl' => $imageUrl,
240
  'universe' => $categoryNames[1],
241
  'category' => end($categoryNames)
242
  );
243
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
 
245
- $gender = 'mister';
246
- if ($quote->getCustomerGender()) {
247
- $gender = $quote->getCustomerGender();
 
 
 
 
 
 
248
  }
 
249
 
 
 
 
 
 
 
 
 
 
250
  $lastname = $quote->getCustomerLastname();
251
  $firstname = $quote->getCustomerFirstname();
 
 
 
 
252
 
 
 
253
  $address = $quote->getBillingAddress();
254
- $phone = ($address)? $address->getTelephone() : $quote->getTelephone();
255
- $country = ($address)? $address->getCountryId() : $quote->getCountryId();
 
 
 
 
 
 
 
256
 
257
- if (!$items) {
258
- return;
 
259
  }
260
-
261
- if (!$accountId = $quote->getCustomerId()) {
262
- $accountId = $quote->getCustomerEmail();
 
 
 
 
 
263
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
 
265
- if (!$accountId && !$phone) {
 
266
  return;
267
  }
268
 
@@ -272,16 +225,17 @@ class Cartsguru_Model_Webservice
272
  'siteId' => $siteId, //SiteId is part of plugin configuration
273
  'id' => $quote->getId(), //Order reference, the same display to the buyer
274
  'creationDate' => $this->formatDate($quote->getCreatedAt()), // Date of the order as string in json format
275
- 'totalET' => (float)$quote->getSubtotal(), // Amount excluded taxes and excluded shipping
 
 
276
  'accountId' => $accountId, // Account id of the buyer
277
  'civility' => $gender, // Use string in this list : 'mister','madam','miss'
278
- 'ip' => $quote->getRemoteIp(),
279
  'lastname' => $this->notEmpty($lastname), // Lastname of the buyer
280
  'firstname' => $this->notEmpty($firstname), // Firstname of the buyer
281
- 'email' => $this->notEmpty($quote->getCustomerEmail()), // Email of the buyer
282
  'phoneNumber' => $this->notEmpty($phone), // Landline phone number of buyer (internationnal format)
283
- 'countryCode' => $this->notEmpty($country),
284
- 'items' => $items
285
  );
286
  }
287
 
@@ -291,7 +245,13 @@ class Cartsguru_Model_Webservice
291
  */
292
  public function sendAbadonnedCart($quote)
293
  {
 
294
  $cartData = $this->getAbadonnedCartData($quote);
 
 
 
 
 
295
  $cache = Mage::app()->getCache();
296
  $data = $cache->load(md5(json_encode($cartData)));
297
  if (empty($data)) {
@@ -301,68 +261,37 @@ class Cartsguru_Model_Webservice
301
  }
302
  }
303
  }
304
-
305
  /**
306
- * get customer Firstname
307
  * @param $customer
308
  * @return string
309
  */
310
- public function getFirstname($customer)
311
  {
312
  $firstname = $customer->getFirstname();
313
- if (!$firstname) {
314
- $address = $customer->getDefaultBillingAddress();
315
- if ($address) {
316
- return $address->getFirstname();
317
- }
318
  }
319
 
320
  return $firstname;
321
  }
322
 
323
  /**
324
- * get customer Lastname
325
  * @param $customer
326
  * @return string
327
  */
328
- public function getLastname($customer)
329
  {
330
  $lastname = $customer->getLastname();
331
- if (!$lastname) {
332
- $address = $customer->getDefaultBillingAddress();
333
- if ($address) {
334
- return $address->getLastname();
335
- }
336
  }
337
 
338
  return $lastname;
339
  }
340
 
341
- /**
342
- * get customer gender and format it
343
- * @param $customer
344
- * @return string
345
- */
346
- public function getGender($customer)
347
- {
348
- return ($customer->getGender())?
349
- $customer->getGender()
350
- : 'mister';
351
- }
352
-
353
- /**
354
- * get customer Phone
355
- * @param $customer
356
- * @return string
357
- */
358
- public function getPhone($customer)
359
- {
360
- $address = $customer->getDefaultBillingAddress();
361
- return ($address)?
362
- $address->getPhone()
363
- : '';
364
- }
365
-
366
  /**
367
  * This method get customer data in cartsguru api format
368
  * @param $customer
@@ -370,12 +299,13 @@ class Cartsguru_Model_Webservice
370
  */
371
  public function getCustomerData($customer)
372
  {
373
- $gender = $this->getGender($customer);
374
- $lastname = $this->getLastname($customer);
375
- $firstname = $this->getFirstname($customer);
 
 
376
  $phone = '';
377
  $country = '';
378
- $address = $customer->getDefaultBillingAddress();
379
  if ($address) {
380
  $phone = $address->getTelephone();
381
  $country = $address->getCountryId();
@@ -403,4 +333,47 @@ class Cartsguru_Model_Webservice
403
  $customerData = $this->getCustomerData($customer);
404
  $this->doPostRequest('/accounts', $customerData);
405
  }
406
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  class Cartsguru_Model_Webservice
8
  {
9
  private $apiBaseUrl = 'https://api.carts.guru';
10
+
11
  /**
12
  * If value is empty return ''
13
  * @param $value
18
  return ($value)? $value : '';
19
  }
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  /**
22
  * This method format date in json format
23
  * @param $date
27
  {
28
  return date('Y-m-d\TH:i:sP', strtotime($date));
29
  }
30
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  /**
32
  * Get category names
33
  * @param $item
56
 
57
  return $categoryNames;
58
  }
59
+
60
  /**
61
+ * This method calculate total taxes included, shipping excluded
62
+ * @param $obj order or quote
63
+ * @return float
64
  */
65
+ public function getTotalATI($obj){
66
+ return (float)$obj->getGrandTotal() - (float)$obj->getShippingAmount() - (float)$obj->getShippingTaxAmount();
67
+ }
68
+
69
+ /**
70
+ * This method build items from order or quote
71
+ * @param $obj order or quote
72
+ * @return array
73
+ */
74
+ public function getItemsData($obj){
75
  $items = array();
76
+ foreach ($obj->getAllVisibleItems() as $item) {
 
77
  $product=$item->getProduct();
78
  $product=$product->load($product->getId());
79
  if($product->getImage() && $product->getImage() != 'no_selection') {
87
  $imageUrl=$this->notEmpty(null);
88
  }
89
  $categoryNames = $this->getCatNames($item);
90
+
91
+ $quantity = (int)$item->getQtyOrdered();
92
+ if ($quantity == 0){
93
+ $quantity = (int)$item->getQty();
94
+ }
95
+
96
  $items[] = array(
97
+ 'id' => $item->getId(), // SKU or product id
98
  'label' => $item->getName(), // Designation
99
+ 'quantity' => $quantity, // Count
100
+ 'totalET' => (float)$item->getPrice()*$quantity, // Subtotal of item, taxe excluded
101
+ 'totalATI' => (float)$item->getPriceInclTax()*$quantity, // Subtotal of item, taxe included
102
  'url' => $item->getProduct()->getProductUrl(), // URL of product sheet
103
  'imageUrl' => $imageUrl,
104
  'universe' => $categoryNames[1],
105
  'category' => end($categoryNames)
106
  );
107
  }
108
+ return $items;
109
+ }
110
+
111
+ /**
112
+ * This method return order data in cartsguru format
113
+ * @param $order
114
+ * @return array
115
+ */
116
+ public function getOrderData($order)
117
+ {
118
+ //Customer data
119
+ $gender = ($order->getCustomerGender()) ? $order->getCustomerGender() : 'mister';
120
+ $email = $order->getCustomerEmail();
121
+ if (!$accountId = $order->getCustomerId()) {
122
+ $accountId = $email;
123
+ }
124
+
125
+ //Address
126
+ $address = $order->getBillingAddress();
127
+
128
+ return array(
129
+ 'siteId' => Mage::getStoreConfig('cartsguru/cartsguru_group/siteid', Mage::app()->getStore()), //Site Id
130
+ 'id' => $order->getIncrementId(), //Order reference, the same display to the buyer
131
+ 'creationDate' => $this->formatDate($order->getCreatedAt()), // Date of the order as string in json format
132
+ 'cartId' => $order->getQuoteId(), // Cart identifier, source of the order
133
+ 'totalET' => (float)$order->getSubtotal(), // Amount excluded taxes and excluded shipping
134
+ 'totalATI' => $this->getTotalATI($order), // Amount included taxes and excluded shipping
135
+ 'state' => $order->getStatus(), // raw order status
136
+ 'accountId' => $accountId, // Account id of the buyer
137
+ 'ip' => $order->getRemoteIp(), // User IP
138
+ 'civility' => $this->notEmpty($gender), // Use string in this list : 'mister','madam','miss'
139
+ 'lastname' => $this->notEmpty($address->getLastname()), // Lastname of the buyer
140
+ 'firstname' => $this->notEmpty($address->getFirstname()), // Firstname of the buyer
141
+ 'email' => $this->notEmpty($email), // Email of the buye
142
+ 'phoneNumber' => $this->notEmpty($address->getTelephone()), // Landline phone number of buyer (internationnal format)
143
+ 'countryCode' => $this->notEmpty($address->getCountryId()), // Country code of buyer
144
+ 'items' => $this->getItemsData($order) // Details
145
+ );
146
+ }
147
 
148
+ /**
149
+ * This method send order data by api
150
+ * @param $order
151
+ */
152
+ public function sendOrder($order)
153
+ {
154
+ $orderData = $this->getOrderData($order);
155
+ if (!empty($orderData)) {
156
+ $this->doPostRequest('/orders', $orderData);
157
  }
158
+ }
159
 
160
+ /**
161
+ * This method return abounded cart data in cartsguru api format
162
+ * @param $quote
163
+ * @return array|void
164
+ */
165
+ public function getAbadonnedCartData($quote)
166
+ {
167
+ //Customer data
168
+ $gender = ($quote->getCustomerGender()) ? $quote->getCustomerGender() : 'mister';
169
  $lastname = $quote->getCustomerLastname();
170
  $firstname = $quote->getCustomerFirstname();
171
+ $email = $quote->getCustomerEmail();
172
+ if (!$accountId = $quote->getCustomerId()) {
173
+ $accountId = $email;
174
+ }
175
 
176
+ //Lookup for phone & country
177
+ $customer = $quote->getCustomer();
178
  $address = $quote->getBillingAddress();
179
+ $request = Mage::app()->getRequest()->getParams();
180
+
181
+ $phone = '';
182
+ $country = '';
183
+
184
+ if (isset($request['billing'])) {
185
+ if (isset($request['billing']['telephone'])) {
186
+ $phone = $request['billing']['telephone'];
187
+ }
188
 
189
+ if (isset($request['billing']['country_id'])) {
190
+ $country = $request['billing']['country_id'];
191
+ }
192
  }
193
+
194
+ if ($address){
195
+ if (!$phone){
196
+ $phone = $address->getTelephone();
197
+ }
198
+ if (!$country){
199
+ $country = $address->getCountryId();
200
+ }
201
  }
202
+
203
+ if ($customer){
204
+ $customerAddress = $customer->getDefaultBillingAddress();
205
+
206
+ if ($customerAddress && !$phone){
207
+ $phone = $customerAddress->getTelephone();
208
+ }
209
+ if ($customerAddress && !$country){
210
+ $country = $customerAddress->getCountryId();
211
+ }
212
+ }
213
+
214
+ //Items details
215
+ $items = $this->getItemsData($quote);
216
 
217
+ //Check is valid
218
+ if (!$items || (!$accountId && (!$phone || !$email))) {
219
  return;
220
  }
221
 
225
  'siteId' => $siteId, //SiteId is part of plugin configuration
226
  'id' => $quote->getId(), //Order reference, the same display to the buyer
227
  'creationDate' => $this->formatDate($quote->getCreatedAt()), // Date of the order as string in json format
228
+ 'totalET' => (float)$quote->getSubtotal(), // Amount excluded taxes and excluded shipping
229
+ 'totalATI' => $this->getTotalATI($quote), // Amount included taxes and excluded shipping
230
+ 'ip' => $quote->getRemoteIp(), // User IP
231
  'accountId' => $accountId, // Account id of the buyer
232
  'civility' => $gender, // Use string in this list : 'mister','madam','miss'
 
233
  'lastname' => $this->notEmpty($lastname), // Lastname of the buyer
234
  'firstname' => $this->notEmpty($firstname), // Firstname of the buyer
235
+ 'email' => $this->notEmpty($email), // Email of the buyer
236
  'phoneNumber' => $this->notEmpty($phone), // Landline phone number of buyer (internationnal format)
237
+ 'countryCode' => $this->notEmpty($country), // Country code of the buyer
238
+ 'items' => $items // Details
239
  );
240
  }
241
 
245
  */
246
  public function sendAbadonnedCart($quote)
247
  {
248
+ //Get data and continue only if exist
249
  $cartData = $this->getAbadonnedCartData($quote);
250
+ if (!$cartData){
251
+ return;
252
+ }
253
+
254
+ //Check not already sent
255
  $cache = Mage::app()->getCache();
256
  $data = $cache->load(md5(json_encode($cartData)));
257
  if (empty($data)) {
261
  }
262
  }
263
  }
264
+
265
  /**
266
+ * Get customer Firstname
267
  * @param $customer
268
  * @return string
269
  */
270
+ public function getFirstname($customer, $address)
271
  {
272
  $firstname = $customer->getFirstname();
273
+ if (!$firstname && $address) {
274
+ $firstname = $address->getFirstname();
 
 
 
275
  }
276
 
277
  return $firstname;
278
  }
279
 
280
  /**
281
+ * Get customer Lastname
282
  * @param $customer
283
  * @return string
284
  */
285
+ public function getLastname($customer, $address)
286
  {
287
  $lastname = $customer->getLastname();
288
+ if (!$lastname && $address) {
289
+ $lastname = $address->getLastname();
 
 
 
290
  }
291
 
292
  return $lastname;
293
  }
294
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295
  /**
296
  * This method get customer data in cartsguru api format
297
  * @param $customer
299
  */
300
  public function getCustomerData($customer)
301
  {
302
+ $address = $customer->getDefaultBillingAddress();
303
+
304
+ $gender = ($customer->getGender())? $customer->getGender() : 'mister';
305
+ $lastname = $this->getLastname($customer, $address);
306
+ $firstname = $this->getFirstname($customer, $address);
307
  $phone = '';
308
  $country = '';
 
309
  if ($address) {
310
  $phone = $address->getTelephone();
311
  $country = $address->getCountryId();
333
  $customerData = $this->getCustomerData($customer);
334
  $this->doPostRequest('/accounts', $customerData);
335
  }
336
+
337
+
338
+
339
+ /** This method return true if connect to server is ok
340
+ * @return bool
341
+ */
342
+ public function checkAddress()
343
+ {
344
+ $baseUrl = Mage::getBaseUrl() . 'api/rest';
345
+ $fields = array(
346
+ 'plugin' => 'magento',
347
+ 'pluginVersion' => '1.1.3',
348
+ 'storeVersion' => Mage::getVersion()
349
+ );
350
+ $siteId = Mage::getStoreConfig('cartsguru/cartsguru_group/siteid', Mage::app()->getStore());
351
+ $requestUrl = '/sites/' . $siteId . '/register-plugin';
352
+
353
+ $response = $this->doPostRequest($requestUrl, $fields);
354
+ return ($response)?
355
+ ($response->getStatus() == 200)
356
+ : false;
357
+ }
358
+
359
+ /**
360
+ * This method send data on api path
361
+ * @param $apiPath
362
+ * @param $fields
363
+ * @return Zend_Http_Response
364
+ */
365
+ protected function doPostRequest($apiPath, $fields)
366
+ {
367
+ try {
368
+ $url = $this->apiBaseUrl . $apiPath;
369
+ $client = new Zend_Http_Client($url);
370
+ $client->setHeaders('x-auth-key', Mage::getStoreConfig('cartsguru/cartsguru_group/auth', Mage::app()->getStore()));
371
+ $client->setUri($url);
372
+ $client->setRawData(json_encode($fields), 'application/json');
373
+ $response = $client->request(Zend_Http_Client::POST);
374
+ } catch (Exception $e) {}
375
+
376
+ return $response;
377
+ }
378
+
379
+ }
app/code/local/Cartsguru/etc/config.xml CHANGED
@@ -3,7 +3,7 @@
3
  <!-- plugin name -->
4
  <modules>
5
  <Cartsguru>
6
- <version>1.1.2</version>
7
  </Cartsguru>
8
  </modules>
9
  <global>
3
  <!-- plugin name -->
4
  <modules>
5
  <Cartsguru>
6
+ <version>1.1.3</version>
7
  </Cartsguru>
8
  </modules>
9
  <global>
package.xml CHANGED
@@ -1,7 +1,7 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>cartsguru</name>
4
- <version>1.1.2</version>
5
  <stability>stable</stability>
6
  <license uri="http://opensource.org/licenses/gpl-license.php">GPL</license>
7
  <channel>community</channel>
@@ -18,11 +18,13 @@ Effortlessly reduce the number of abandoned shopping carts by automating telepho
18
  &#xD;
19
  - SMS Callback &amp;amp; Push SMS&#xD;
20
  Send to your prospective customers having abandoned a cart, an SMS suggesting a free call back from your customer relations service, a straightforward SMS reminder or an SMS offering a personalized discount</description>
21
- <notes>- Improve get address support </notes>
 
 
22
  <authors><author><name>Maxime Pruvost</name><user>cgmaximepruvost</user><email>maxime@carts.guru</email></author></authors>
23
- <date>2016-04-01</date>
24
- <time>16:46:39</time>
25
- <contents><target name="magelocal"><dir name="Cartsguru"><dir name="Helper"><file name="Data.php" hash="f6590d08ba862a169ce43459ddb1193c"/></dir><dir name="Model"><file name="Observer.php" hash="9fddb0f5bac304236535c31f1bb251ed"/><dir name="Sales"><dir name="Order"><dir name="Api"><file name="V2.php" hash="a3b05d6f3931a1be1b2e92618095a123"/></dir></dir></dir><file name="Webservice.php" hash="2c0a921b5639af87cccecdd59da74d7d"/></dir><dir name="etc"><file name="config.xml" hash="ced1932ad041c16e9c9d84531fd44476"/><file name="system.xml" hash="cb0fbf86d2be47dbd719739ee79c4cba"/></dir><dir name="sql"><dir name="cartsguru_setup"><file name="install-1.0.0.php" hash="c46126ce8ce549d525fb25bddc5090b5"/></dir></dir></dir></target><target name="magelocale"><dir name="fr_FR"><file name="CartsGuru.csv" hash="b6d51893c33ddef1d53372d3a23b036c"/></dir><dir name="en_US"><file name="CartsGuru.csv" hash="921cb4133db47471456759443bb269f5"/></dir></target><target name="mageetc"><dir name="modules"><file name="Cartsguru.xml" hash="32bfa7d63b1a5b6b8f7977bf31af4e28"/></dir></target></contents>
26
  <compatible/>
27
  <dependencies><required><php><min>5.3.0</min><max>6.0.0</max></php></required></dependencies>
28
  </package>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>cartsguru</name>
4
+ <version>1.1.3</version>
5
  <stability>stable</stability>
6
  <license uri="http://opensource.org/licenses/gpl-license.php">GPL</license>
7
  <channel>community</channel>
18
  &#xD;
19
  - SMS Callback &amp;amp; Push SMS&#xD;
20
  Send to your prospective customers having abandoned a cart, an SMS suggesting a free call back from your customer relations service, a straightforward SMS reminder or an SMS offering a personalized discount</description>
21
+ <notes>- Refactor address management&#xD;
22
+ - Remove additionnal fields&#xD;
23
+ - Add totalATI in sent data</notes>
24
  <authors><author><name>Maxime Pruvost</name><user>cgmaximepruvost</user><email>maxime@carts.guru</email></author></authors>
25
+ <date>2016-04-05</date>
26
+ <time>11:26:16</time>
27
+ <contents><target name="magelocal"><dir name="Cartsguru"><dir name="Helper"><file name="Data.php" hash="f6590d08ba862a169ce43459ddb1193c"/></dir><dir name="Model"><file name="Observer.php" hash="0c8290d79f166c3fb9931994477a059d"/><dir name="Sales"><dir name="Order"><dir name="Api"><file name="V2.php" hash="a3b05d6f3931a1be1b2e92618095a123"/></dir></dir></dir><file name="Webservice.php" hash="b75e6b8fc71328ca86839932b9effbb3"/></dir><dir name="etc"><file name="config.xml" hash="12199f27c585a6f7e15ffe232f7e1b0f"/><file name="system.xml" hash="cb0fbf86d2be47dbd719739ee79c4cba"/></dir><dir name="sql"><dir name="cartsguru_setup"><file name="install-1.0.0.php" hash="c46126ce8ce549d525fb25bddc5090b5"/></dir></dir></dir></target><target name="magelocale"><dir name="fr_FR"><file name="CartsGuru.csv" hash="b6d51893c33ddef1d53372d3a23b036c"/></dir><dir name="en_US"><file name="CartsGuru.csv" hash="921cb4133db47471456759443bb269f5"/></dir></target><target name="mageetc"><dir name="modules"><file name="Cartsguru.xml" hash="32bfa7d63b1a5b6b8f7977bf31af4e28"/></dir></target></contents>
28
  <compatible/>
29
  <dependencies><required><php><min>5.3.0</min><max>6.0.0</max></php></required></dependencies>
30
  </package>