GreenRope_OrderExport - Version 1.0.0.1

Version Notes

You need to have a GreenRope account to make use of this extension. You may sign up for one at http://www.greenrope.com.

Download this release

Release Info

Developer Daniel Watkins
Extension GreenRope_OrderExport
Version 1.0.0.1
Comparing to
See all releases


Version 1.0.0.1

app/code/community/GreenRope/OrderExport/Helper/Data.php ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
1
+ <?php
2
+ class GreenRope_OrderExport_Helper_Data extends Mage_Core_Helper_Abstract
3
+ {
4
+ }
5
+ ?>
app/code/community/GreenRope/OrderExport/Model/Export.php ADDED
@@ -0,0 +1,881 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class GreenRope_CartItem
4
+ {
5
+ private $itemID;
6
+ private $itemQuantity;
7
+ private $itemSubtotal;
8
+
9
+ public function getItemID()
10
+ {
11
+ return $this->itemID;
12
+ }
13
+
14
+ public function setItemID($new_itemID)
15
+ {
16
+ $this->itemID = $new_itemID;
17
+ }
18
+
19
+ public function getItemQuantity()
20
+ {
21
+ return $this->itemQuantity;
22
+ }
23
+
24
+ public function setItemQuantity($new_itemQuantity)
25
+ {
26
+ $this->itemQuantity = $new_itemQuantity;
27
+ }
28
+
29
+ public function getItemSubtotal()
30
+ {
31
+ return $this->itemSubtotal;
32
+ }
33
+
34
+ public function setItemSubtotal($new_itemSubtotal)
35
+ {
36
+ $this->itemSubtotal = $new_itemSubtotal;
37
+ }
38
+ }
39
+
40
+ class GreenRope_OrderExport_Model_Export
41
+ {
42
+
43
+ /**
44
+ * Generates an XML file from the order data and places it into
45
+ * the var/export directory
46
+ *
47
+ * @param Mage_Sales_Model_Order $order order object
48
+ *
49
+ * @return boolean
50
+ */
51
+ public function exportOrder($order)
52
+ {
53
+ $dirPath = Mage::getBaseDir('var') . DS . 'export';
54
+ $filePath = $dirPath. DS . $order->getIncrementId() . '.txt';
55
+
56
+ // Initial connection to GreenRope
57
+ $greenrope_url = "https://api.stgi.net/xml.pl";
58
+ $API_UserName = urlencode(Mage::getStoreConfig('greenrope/settings/GreenRopeUsername'));
59
+ $API_Password = urlencode(Mage::getStoreConfig('greenrope/settings/GreenRopePassword'));
60
+ $GreenRopeGroup = Mage::getStoreConfig('greenrope/settings/GreenRopeGroup');
61
+
62
+ $API_XML = "<GetAuthTokenRequest>\n";
63
+ $API_XML .= "</GetAuthTokenRequest>\n";
64
+
65
+ $API_XML = urlencode($API_XML);
66
+
67
+ $ch = curl_init();
68
+ curl_setopt($ch, CURLOPT_URL, $greenrope_url);
69
+ curl_setopt($ch, CURLOPT_VERBOSE, 1);
70
+
71
+ // turning off the server and peer verification(TrustManager Concept).
72
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
73
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
74
+
75
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
76
+ curl_setopt($ch, CURLOPT_POST, 1);
77
+
78
+ // NVPRequest for submitting to server
79
+ $nvpreq = "email=$API_UserName&password=$API_Password&xml=$API_XML";
80
+
81
+ // setting the nvpreq as POST FIELD to curl
82
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
83
+
84
+ // getting response from server
85
+ $httpResponse = curl_exec($ch);
86
+
87
+ $my_reader = new XMLReader();
88
+ $my_reader->xml($httpResponse);
89
+
90
+ $xml_result = "";
91
+ $xml_token = "";
92
+ $xml_errorcode = "";
93
+ $xml_errormsg = "";
94
+
95
+ while ($my_reader->read())
96
+ {
97
+ switch ($my_reader->nodeType)
98
+ {
99
+ case XMLReader::ELEMENT:
100
+ if ($my_reader->name == "Result")
101
+ {
102
+ $my_reader->read();
103
+ $xml_result = $my_reader->value;
104
+ }
105
+ else if ($my_reader->name == "Token")
106
+ {
107
+ $my_reader->read();
108
+ $xml_token = $my_reader->value;
109
+ }
110
+ else if ($my_reader->name == "ErrorCode")
111
+ {
112
+ $my_reader->read();
113
+ $xml_errorcode = $my_reader->value;
114
+ }
115
+ else if ($my_reader->name == "ErrorText")
116
+ {
117
+ $my_reader->read();
118
+ $xml_errormsg = $my_reader->value;
119
+ }
120
+
121
+ break;
122
+ }
123
+ }
124
+
125
+ if ($xml_result == "Error")
126
+ {
127
+ Mage::log(
128
+ "Error - error code was $xml_errorcode, error message was $xml_errormsg\n",
129
+ Zend_Log::DEBUG,
130
+ 'order_export.log',
131
+ true
132
+ );
133
+ }
134
+ else if ($xml_result == "Success")
135
+ {
136
+ $API_Token = urlencode($xml_token);
137
+
138
+ // Get the group ID
139
+ $API_XML_String = "<GetGroupsRequest group_name=\"" . $GreenRopeGroup . "\">\n";
140
+ $API_XML_String .= "</GetGroupsRequest>\n";
141
+
142
+ $API_XML = urlencode($API_XML_String);
143
+
144
+ $API_XML = urlencode($API_XML_String);
145
+
146
+ // NVPRequest for submitting to server
147
+ $nvpreq = "email=$API_UserName&auth_token=$API_Token&xml=$API_XML";
148
+
149
+ $httpResponse = $this->Send_CE_XML_Request($nvpreq); // getting response from server
150
+
151
+ Mage::log(
152
+ "Response for GetGroupsRequest was $httpResponse\n",
153
+ Zend_Log::DEBUG,
154
+ 'order_export.log',
155
+ true
156
+ );
157
+
158
+ $GetGroupsResponse = new SimpleXMLElement($httpResponse);
159
+
160
+ $response_result = $GetGroupsResponse->Result;
161
+
162
+ Mage::log(
163
+ "Result for GetGroupsResponse was $response_result\n",
164
+ Zend_Log::DEBUG,
165
+ 'order_export.log',
166
+ true
167
+ );
168
+
169
+ if ($response_result != 'Success')
170
+ {
171
+ Mage::log(
172
+ "Error - could not find group\n",
173
+ Zend_Log::DEBUG,
174
+ 'order_export.log',
175
+ true
176
+ );
177
+
178
+ return true;
179
+ }
180
+
181
+ $GreenRopeGroupID = $GetGroupsResponse->Groups->Group[0]->Group_id;
182
+
183
+ $GreenRopeCartItems = array();
184
+
185
+ foreach ($order->getAllItems() as $item)
186
+ {
187
+ // if (!isset($sku_counter[$item->getSku()])) {
188
+ // $sku_counter[$item->getSku()] = 0;
189
+
190
+ $item_summary_string = '';
191
+
192
+ $item_summary_string .= 'Item ID: ' . $item->getProductId() . "\n";
193
+ $item_summary_string .= 'Item SKU: ' . $item->getSku() . "\n";
194
+ $item_summary_string .= 'Item Name: ' . $item->getName() . "\n";
195
+
196
+ $my_product = Mage::getModel('catalog/product')->load($item->getProductId());
197
+
198
+ $my_categories = $my_product->getCategoryIds();
199
+ $product_category = '';
200
+ $product_category_id = '';
201
+
202
+ foreach ($my_categories as $my_cat_id)
203
+ {
204
+ $my_category = Mage::getModel('catalog/category')->load($my_cat_id);
205
+ $item_summary_string .= 'Item Category: ' . $my_category->getName() . "\n";
206
+
207
+ if ($my_category->getName() != '')
208
+ {
209
+ $product_category = $my_category->getName();
210
+ break;
211
+ }
212
+ }
213
+
214
+ $category_id = '';
215
+
216
+ if (!(empty($product_category)))
217
+ {
218
+ // See if this category exists
219
+ $API_XML_String = "<GetStoreItemCategoriesRequest group_name=\"" . $GreenRopeGroup . "\" category_name=\"" . $product_category . "\" >\n";
220
+ $API_XML_String .= "</GetStoreItemCategoriesRequest>\n";
221
+
222
+ $API_XML = urlencode($API_XML_String);
223
+
224
+ // NVPRequest for submitting to server
225
+ $nvpreq = "email=$API_UserName&auth_token=$API_Token&xml=$API_XML";
226
+
227
+ $httpResponse = $this->Send_CE_XML_Request($nvpreq); // getting response from server
228
+
229
+ Mage::log(
230
+ "Response for GetStoreItemCategories was $httpResponse\n",
231
+ Zend_Log::DEBUG,
232
+ 'order_export.log',
233
+ true
234
+ );
235
+
236
+ $GetStoreItemCategoriesResponse = new SimpleXMLElement($httpResponse);
237
+
238
+ $response_result = $GetStoreItemCategoriesResponse->Result;
239
+
240
+ Mage::log(
241
+ "Result for GetStoreItemCategories was $response_result\n",
242
+ Zend_Log::DEBUG,
243
+ 'order_export.log',
244
+ true
245
+ );
246
+
247
+ if ($response_result != 'Success')
248
+ {
249
+ $response_error_code = $GetStoreItemCategoriesResponse->ErrorCode;
250
+ $response_error_text = $GetStoreItemCategoriesResponse->ErrorText;
251
+
252
+ Mage::log(
253
+ "Error was $response_error_code: $response_error_text\n",
254
+ Zend_Log::DEBUG,
255
+ 'order_export.log',
256
+ true
257
+ );
258
+
259
+ if ($response_error_code == 5001)
260
+ {
261
+ // Add the category
262
+ $API_XML_String = "<AddStoreItemCategoriesRequest>\n";
263
+ $API_XML_String .= "<StoreItemCategories>\n";
264
+ $API_XML_String .= "<StoreItemCategory>\n";
265
+ $API_XML_String .= "<Group_id>" . $GreenRopeGroupID . "</Group_id>\n";
266
+ $API_XML_String .= "<Category_name>" . $product_category . "</Category_name>\n";
267
+ $API_XML_String .= "</StoreItemCategory>\n";
268
+ $API_XML_String .= "</StoreItemCategories>\n";
269
+ $API_XML_String .= "</AddStoreItemCategoriesRequest>\n";
270
+
271
+ $API_XML = urlencode($API_XML_String);
272
+
273
+ // NVPRequest for submitting to server
274
+ $nvpreq = "email=$API_UserName&auth_token=$API_Token&xml=$API_XML";
275
+
276
+ $httpResponse = $this->Send_CE_XML_Request($nvpreq); // getting response from server
277
+
278
+ Mage::log(
279
+ "Response for AddStoreItemCategories was $httpResponse\n",
280
+ Zend_Log::DEBUG,
281
+ 'order_export.log',
282
+ true
283
+ );
284
+
285
+ $AddStoreItemCategoriesResponse = new SimpleXMLElement($httpResponse);
286
+ $response_result = $AddStoreItemCategoriesResponse->StoreItemCategories->StoreItemCategory[0]->Result;
287
+
288
+ if ($response_result <> 'Success')
289
+ {
290
+ Mage::log(
291
+ "Add store item category failed\n",
292
+ Zend_Log::DEBUG,
293
+ 'order_export.log',
294
+ true
295
+ );
296
+
297
+ return true;
298
+ }
299
+
300
+ $category_id = $AddStoreItemCategoriesResponse->StoreItemCategories->StoreItemCategory[0]->Category_id;
301
+
302
+ Mage::log(
303
+ "Added store item category, ID was $category_id\n",
304
+ Zend_Log::DEBUG,
305
+ 'order_export.log',
306
+ true
307
+ );
308
+ }
309
+ else
310
+ {
311
+ Mage::log(
312
+ "Unknown error $response_error_code from GetStoreItemCategories\n",
313
+ Zend_Log::DEBUG,
314
+ 'order_export.log',
315
+ true
316
+ );
317
+
318
+ return true;
319
+ }
320
+ }
321
+ else
322
+ {
323
+ $category_id = $GetStoreItemCategoriesResponse->StoreItemCategories->StoreItemCategory[0]->Category_id;
324
+
325
+ Mage::log(
326
+ "GetStoreItemCategory succeeded, category ID was $category_id\n",
327
+ Zend_Log::DEBUG,
328
+ 'order_export.log',
329
+ true
330
+ );
331
+ }
332
+ }
333
+
334
+ $item_summary_string .= 'Item Description: ' . $my_product->getDescription() . "\n";
335
+ $item_summary_string .= 'Item Price: ' . $item->getPrice() . "\n";
336
+ $item_summary_string .= 'Item Quantity: ' . $item->getQtyOrdered() . "\n";
337
+ $item_summary_string .= "\n\n";
338
+
339
+ $item_description = $my_product->getDescription();
340
+ $item_price = $item->getPrice();
341
+ $item_qty = $item->getQtyOrdered();
342
+
343
+ Mage::log(
344
+ $item_summary_string,
345
+ Zend_Log::DEBUG,
346
+ 'order_export.log',
347
+ true
348
+ );
349
+
350
+ // See if item exists
351
+ $item_name = $item->getName();
352
+ $API_XML_String = "<GetStoreItemsRequest item_name=\"$item_name\" group_id=\"$GreenRopeGroupID\">\n";
353
+ $API_XML_String .= "</GetStoreItemsRequest>\n";
354
+ $API_XML = urlencode($API_XML_String);
355
+
356
+ // NVPRequest for submitting to server
357
+ $nvpreq = "email=$API_UserName&auth_token=$API_Token&xml=$API_XML";
358
+
359
+ $httpResponse = $this->Send_CE_XML_Request($nvpreq); // getting response from server
360
+
361
+ Mage::log(
362
+ "Response for GetStoreItems was $httpResponse\n",
363
+ Zend_Log::DEBUG,
364
+ 'order_export.log',
365
+ true
366
+ );
367
+
368
+ $GetStoreItemsResponse = new SimpleXMLElement($httpResponse);
369
+
370
+ $response_result = $GetStoreItemsResponse->Result;
371
+
372
+ if ($response_result != 'Success')
373
+ {
374
+ $response_error_code = $GetStoreItemsResponse->ErrorCode;
375
+ $response_error_text = $GetStoreItemsResponse->ErrorText;
376
+
377
+ Mage::log(
378
+ "Error was $response_error_code: $response_error_text\n",
379
+ Zend_Log::DEBUG,
380
+ 'order_export.log',
381
+ true
382
+ );
383
+
384
+ if ($response_error_code == 5301)
385
+ {
386
+ // Add the item
387
+ $API_XML_String = "<AddStoreItemsRequest>\n";
388
+ $API_XML_String .= "<StoreItems>\n";
389
+ $API_XML_String .= "<StoreItem>\n";
390
+ $API_XML_String .= "<Group_id>" . $GreenRopeGroupID . "</Group_id>\n";
391
+ $API_XML_String .= "<Item_name>" . $item_name . "</Item_name>\n";
392
+ $API_XML_String .= "<Item_description>" . $item_description . "</Item_description>\n";
393
+ $API_XML_String .= "<Category_id>" . $category_id . "</Category_id>\n";
394
+ $API_XML_String .= "<Item_price>" . $item_price . "</Item_price>\n";
395
+ $API_XML_String .= "</StoreItem>\n";
396
+ $API_XML_String .= "</StoreItems>\n";
397
+ $API_XML_String .= "</AddStoreItemsRequest>\n";
398
+
399
+ $API_XML = urlencode($API_XML_String);
400
+
401
+ // NVPRequest for submitting to server
402
+ $nvpreq = "email=$API_UserName&auth_token=$API_Token&xml=$API_XML";
403
+
404
+ $httpResponse = $this->Send_CE_XML_Request($nvpreq); // getting response from server
405
+
406
+ Mage::log(
407
+ "Response for AddStoreItems was $httpResponse\n",
408
+ Zend_Log::DEBUG,
409
+ 'order_export.log',
410
+ true
411
+ );
412
+
413
+ $AddStoreItemsResponse = new SimpleXMLElement($httpResponse);
414
+ $response_result = $AddStoreItemsResponse->StoreItems->StoreItem[0]->Result;
415
+
416
+ if ($response_result <> 'Success')
417
+ {
418
+ Mage::log(
419
+ "Add store item failed\n",
420
+ Zend_Log::DEBUG,
421
+ 'order_export.log',
422
+ true
423
+ );
424
+
425
+ return true;
426
+ }
427
+
428
+ $item_id = $AddStoreItemsResponse->StoreItems->StoreItem[0]->Item_id;
429
+
430
+ Mage::log(
431
+ "Added store item, ID was $item_id\n",
432
+ Zend_Log::DEBUG,
433
+ 'order_export.log',
434
+ true
435
+ );
436
+ }
437
+ else
438
+ {
439
+ Mage::log(
440
+ "Unknown error $response_error_code from GetStoreItemCategories\n",
441
+ Zend_Log::DEBUG,
442
+ 'order_export.log',
443
+ true
444
+ );
445
+
446
+ return true;
447
+ }
448
+ }
449
+ else
450
+ {
451
+ $item_id = $GetStoreItemsResponse->StoreItems->StoreItem[0]->Item_id;
452
+
453
+ Mage::log(
454
+ "GetStoreItem succeeded, item ID was $item_id\n",
455
+ Zend_Log::DEBUG,
456
+ 'order_export.log',
457
+ true
458
+ );
459
+
460
+ // Compare fields received to see if an update is needed
461
+ $item_needs_update = false;
462
+
463
+ if ($GetStoreItemsResponse->StoreItems->StoreItem[0]->Item_name != $item_name ||
464
+ $GetStoreItemsResponse->StoreItems->StoreItem[0]->Item_description != $item_description ||
465
+ $GetStoreItemsResponse->StoreItems->StoreItem[0]->Item_price != $item_price)
466
+ {
467
+ $API_XML_String = "<EditStoreItemsRequest>\n";
468
+ $API_XML_String .= "<StoreItems>\n";
469
+ $API_XML_String .= "<StoreItem item_id=\"$item_id\">\n";
470
+ $API_XML_String .= "<Item_name>$item_name</Item_name>\n";
471
+ $API_XML_String .= "<Item_description>$item_description</Item_description>\n";
472
+ $API_XML_String .= "<Item_price>$item_price</Item_price>\n";
473
+ $API_XML_String .= "</StoreItem>\n";
474
+ $API_XML_String .= "</StoreItems>\n";
475
+ $API_XML_String .= "</EditStoreItemsRequest>\n";
476
+ $API_XML = urlencode($API_XML_String);
477
+
478
+ // NVPRequest for submitting to server
479
+ $nvpreq = "email=$API_UserName&auth_token=$API_Token&xml=$API_XML";
480
+
481
+ $httpResponse = $this->Send_CE_XML_Request($nvpreq); // getting response from server
482
+
483
+ Mage::log(
484
+ "Response for EditStoreItems was $httpResponse\n",
485
+ Zend_Log::DEBUG,
486
+ 'order_export.log',
487
+ true
488
+ );
489
+
490
+ $EditStoreItemsResponse = new SimpleXMLElement($httpResponse);
491
+
492
+ $response_result = $EditStoreItemsResponse->StoreItems->StoreItem[0]->Result;
493
+
494
+ if ($response_result <> 'Success')
495
+ {
496
+ Mage::log(
497
+ "Edit store item failed\n",
498
+ Zend_Log::DEBUG,
499
+ 'order_export.log',
500
+ true
501
+ );
502
+
503
+ return true;
504
+ }
505
+ }
506
+ }
507
+
508
+ // Save the item into the array for the cart calls later
509
+ $GreenRopeCartItem = new GreenRope_CartItem();
510
+
511
+ $GreenRopeCartItem->setItemID($item_id);
512
+ $GreenRopeCartItem->setItemQuantity($item_qty);
513
+ $GreenRopeCartItem->setItemSubtotal($item_qty * $item_price);
514
+
515
+ $GreenRopeCartItems[] = $GreenRopeCartItem;
516
+ }
517
+
518
+ // Now customer
519
+ if ($order->getCustomerId() === NULL)
520
+ {
521
+ $customer_address = $order->getBillingAddress();
522
+ Mage::log(
523
+ "No customer ID, got billing address from order\n",
524
+ Zend_Log::DEBUG,
525
+ 'order_export.log',
526
+ true
527
+ );
528
+
529
+ $customer_firstname = $order->getCustomerFirstname();
530
+ $customer_lastname = $order->getCustomerLastname();
531
+ $customer_email = $order->getCustomerEmail();
532
+
533
+ $customer_middlename = '';
534
+ $customer_dob = '';
535
+ $customer_gender = '';
536
+ }
537
+ else
538
+ {
539
+ Mage::log(
540
+ "Customer ID found, customer ID was " . $order->getCustomerId() . "\n",
541
+ Zend_Log::DEBUG,
542
+ 'order_export.log',
543
+ true
544
+ );
545
+
546
+ $customer = Mage::getModel('customer/customer')->load($order->getCustomerId());
547
+
548
+ $customer_id = $order->getCustomerId();
549
+ $customer_email = $customer->getEmail();
550
+ $customer_firstname = $customer->getFirstname();
551
+ $customer_middlename = $customer->getMiddlename();
552
+ $customer_lastname = $customer->getLastname();
553
+ $customer_dob = $customer->getdob();
554
+ $customer_gender = $customer->getGender();
555
+
556
+ $customer_address = $customer->getDefaultBillingAddress();
557
+ }
558
+
559
+ $customer_company = $customer_address->getCompany();
560
+
561
+ $street = $customer_address->getStreet();
562
+ $customer_street = $street[0];
563
+ $customer_city = $customer_address->getCity();
564
+ $customer_state = $customer_address->getRegion();
565
+ $customer_zip = $customer_address->getPostcode();
566
+ $customer_country = $customer_address->getCountryId();
567
+ $customer_telephone = $customer_address->getTelephone();
568
+ $customer_fax = $customer_address->getFax();
569
+
570
+ Mage::log(
571
+ "Customer ID was $customer_id, customer email was $customer_email, customer first name = $customer_firstname, customer middle name was $customer_middlename, customer last name was $customer_lastname, customer DOB was $customer_dob, customer gender was $customer_gender, customer company was $customer_company, customer street address was $customer_street, customer city was $customer_city, customer state was $customer_state, customer zip was $customer_zip, customer country was $customer_country, customer telephone was $customer_telephone, customer fax was $customer_fax.\n",
572
+ Zend_Log::DEBUG,
573
+ 'order_export.log',
574
+ true
575
+ );
576
+
577
+ // Try adding the customer. If it already exists, update it.
578
+ $API_XML_String = "<AddContactsRequest>\n";
579
+ $API_XML_String .= "<Contacts>\n";
580
+ $API_XML_String .= "<Contact>\n";
581
+ $API_XML_String .= "<Firstname>$customer_firstname</Firstname>\n";
582
+ $API_XML_String .= "<Lastname>$customer_lastname</Lastname>\n";
583
+ $API_XML_String .= "<Address1>$customer_street</Address1>\n";
584
+ $API_XML_String .= "<City>$customer_city</City>\n";
585
+ $API_XML_String .= "<State>$customer_state</State>\n";
586
+ $API_XML_String .= "<Zip>$customer_zip</Zip>\n";
587
+ $API_XML_String .= "<Country>$customer_country</Country>\n";
588
+
589
+ if (!(empty($customer_telephone)))
590
+ {
591
+ $API_XML_String .= "<Phone>$customer_telephone</Phone>\n";
592
+ }
593
+
594
+ if (!(empty($customer_fax)))
595
+ {
596
+ $API_XML_String .= "<Fax>$customer_fax</Fax>\n";
597
+ }
598
+
599
+ $API_XML_String .= "<Email>$customer_email</Email>\n";
600
+
601
+ if (!(empty($customer_dob)))
602
+ {
603
+ $API_XML_String .= "<Birthdate>$customer_dob</Birthdate>\n";
604
+ }
605
+
606
+ if (!(empty($customer_gender)))
607
+ {
608
+ $API_XML_String .= "<Gender>$customer_gender</Gender>\n";
609
+ }
610
+
611
+ $API_XML_String .= "<Groups>\n";
612
+ $API_XML_String .= "<Group>$GreenRopeGroup</Group>\n";
613
+ $API_XML_String .= "</Groups>\n";
614
+ $API_XML_String .= "</Contact>\n";
615
+ $API_XML_String .= "</Contacts>\n";
616
+ $API_XML_String .= "</AddContactsRequest>\n";
617
+
618
+ $API_XML = urlencode($API_XML_String);
619
+
620
+ // NVPRequest for submitting to server
621
+ $nvpreq = "email=$API_UserName&auth_token=$API_Token&xml=$API_XML";
622
+
623
+ $httpResponse = $this->Send_CE_XML_Request($nvpreq); // getting response from server
624
+
625
+ Mage::log(
626
+ "Response for AddContacts was $httpResponse\n",
627
+ Zend_Log::DEBUG,
628
+ 'order_export.log',
629
+ true
630
+ );
631
+
632
+ $AddContactsResponse = new SimpleXMLElement($httpResponse);
633
+ $response_result = $AddContactsResponse->Contacts->Contact[0]->Result;
634
+
635
+ if ($response_result <> 'Success')
636
+ {
637
+ if (substr($AddContactsResponse->Contacts->Contact[0]->ErrorText, 0, 36) == 'This contact already exists with ID ')
638
+ {
639
+ $gr_contact_id = substr($AddContactsResponse->Contacts->Contact[0]->ErrorText, 36);
640
+
641
+ // Try adding the group
642
+ $API_XML_String = "<AddContactsToGroupRequest group_name=\"" . $GreenRopeGroup . "\">\n";
643
+ $API_XML_String .= "<Contacts>\n";
644
+ $API_XML_String .= "<Contact contact_id=\"$gr_contact_id\" />\n";
645
+ $API_XML_String .= "</Contacts>\n";
646
+ $API_XML_String .= "</AddContactsToGroupRequest>\n";
647
+
648
+ $API_XML = urlencode($API_XML_String);
649
+
650
+ // NVPRequest for submitting to server
651
+ $nvpreq = "email=$API_UserName&auth_token=$API_Token&xml=$API_XML";
652
+
653
+ $httpResponse = $this->Send_CE_XML_Request($nvpreq); // getting response from server
654
+
655
+ Mage::log(
656
+ "Response for AddContactsToGroup was $httpResponse\n",
657
+ Zend_Log::DEBUG,
658
+ 'order_export.log',
659
+ true
660
+ );
661
+
662
+ $AddContactsToGroupResponse = new SimpleXMLElement($httpResponse);
663
+ $response_result = $AddContactsToGroupResponse->Contacts->Contact[0]->Result;
664
+
665
+ if ($response_result == 'Success')
666
+ {
667
+ Mage::log(
668
+ "Successfully added contact to group $GreenRopeGroup\n",
669
+ Zend_Log::DEBUG,
670
+ 'order_export.log',
671
+ true
672
+ );
673
+ }
674
+ else
675
+ {
676
+ $response_error = $AddContactsToGroupResponse->Contacts->Contact[0]->ErrorText;
677
+
678
+ Mage::log(
679
+ "Error adding contact to group $GreenRopeGroup, error was $response_error\n",
680
+ Zend_Log::DEBUG,
681
+ 'order_export.log',
682
+ true
683
+ );
684
+ }
685
+ }
686
+ else
687
+ {
688
+ Mage::log(
689
+ "Unrecognized Error Response for AddContacts; error response was " . $AddContactsResponse->Contacts->Contact[0]->ErrorText . "\n",
690
+ Zend_Log::DEBUG,
691
+ 'order_export.log',
692
+ true
693
+ );
694
+
695
+ return true;
696
+ }
697
+ }
698
+ else
699
+ {
700
+ $gr_contact_id = $AddContactsResponse->Contacts->Contact[0]->Contact_id;
701
+
702
+ Mage::log(
703
+ "Successfully added contact, new ID was $gr_contact_id\n",
704
+ Zend_Log::DEBUG,
705
+ 'order_export.log',
706
+ true
707
+ );
708
+ }
709
+
710
+ // Now do the cart & transaction
711
+ $orderID = $order->getIncrementId();
712
+
713
+ // Cart first
714
+
715
+ if (count($GreenRopeCartItems) > 0)
716
+ {
717
+ $currenttime = date("Y-m-d H:i:s", Mage::getModel('core/date')->timestamp(time()));
718
+
719
+ $API_XML_String = "<AddStoreShoppingCartRequest>\n";
720
+ $API_XML_String .= "<Group_id>$GreenRopeGroupID</Group_id>\n";
721
+ $API_XML_String .= "<CreationDate>$currenttime</CreationDate>\n";
722
+ $API_XML_String .= "<Items>\n";
723
+
724
+ foreach ($GreenRopeCartItems as $GreenRopeItem)
725
+ {
726
+ $API_XML_String .= "<Item>\n";
727
+ $API_XML_String .= "<Item_id>" . $GreenRopeItem->getItemID() . "</Item_id>\n";
728
+ $API_XML_String .= "<Quantity>" . $GreenRopeItem->getItemQuantity() . "</Quantity>\n";
729
+ $API_XML_String .= "<SubtotalAmount>" . $GreenRopeItem->getItemSubtotal() . "</SubtotalAmount>\n";
730
+ $API_XML_String .= "</Item>\n";
731
+ }
732
+
733
+ $API_XML_String .= "</Items>\n";
734
+ $API_XML_String .= "</AddStoreShoppingCartRequest>\n";
735
+
736
+ Mage::log(
737
+ "Request for AddStoreShoppingCart was $API_XML_String\n",
738
+ Zend_Log::DEBUG,
739
+ 'order_export.log',
740
+ true
741
+ );
742
+
743
+ $API_XML = urlencode($API_XML_String);
744
+ // NVPRequest for submitting to server
745
+ $nvpreq = "email=$API_UserName&auth_token=$API_Token&xml=$API_XML";
746
+
747
+ $httpResponse = $this->Send_CE_XML_Request($nvpreq); // getting response from server
748
+
749
+ Mage::log(
750
+ "Response for AddStoreShoppingCart was $httpResponse\n",
751
+ Zend_Log::DEBUG,
752
+ 'order_export.log',
753
+ true
754
+ );
755
+
756
+ $AddStoreShoppingCartResponse = new SimpleXMLElement($httpResponse);
757
+ $response_result = $AddStoreShoppingCartResponse->Result;
758
+
759
+ if ($response_result == 'Success')
760
+ {
761
+ $GR_Cart_ID = $AddStoreShoppingCartResponse->Cart_id;
762
+
763
+ Mage::log(
764
+ "Successfully added cart, ID was $GR_Cart_ID\n",
765
+ Zend_Log::DEBUG,
766
+ 'order_export.log',
767
+ true
768
+ );
769
+
770
+ // Now add the purchase
771
+ $API_XML_String = "<AddStorePurchaseRequest>\n";
772
+ $API_XML_String .= "<Group_id>$GreenRopeGroupID</Group_id>\n";
773
+ $API_XML_String .= "<Cart_id>$GR_Cart_ID</Cart_id>\n";
774
+ $API_XML_String .= "<CreationDate>$currenttime</CreationDate>\n";
775
+ $API_XML_String .= "<PurchaserEmail>$customer_email</PurchaserEmail>\n";
776
+ $API_XML_String .= "<PurchaserIP>" . $_SERVER['REMOTE_ADDR'] . "</PurchaserIP>\n";
777
+ $API_XML_String .= "<TransactionDate>" . $currenttime . "</TransactionDate>\n";
778
+ $API_XML_String .= "<TransactionAmount>" . $order->getGrandTotal() . "</TransactionAmount>\n";
779
+ $API_XML_String .= "<Transaction_id>" . "Magento" . $orderID . "</Transaction_id>\n";
780
+ $API_XML_String .= "</AddStorePurchaseRequest>\n";
781
+
782
+ Mage::log(
783
+ "Request for AddStorePurchase was $API_XML_String\n",
784
+ Zend_Log::DEBUG,
785
+ 'order_export.log',
786
+ true
787
+ );
788
+
789
+ $API_XML = urlencode($API_XML_String);
790
+
791
+ // NVPRequest for submitting to server
792
+ $nvpreq = "email=$API_UserName&auth_token=$API_Token&xml=$API_XML";
793
+
794
+ $httpResponse = $this->Send_CE_XML_Request($nvpreq); // getting response from server
795
+
796
+ Mage::log(
797
+ "Response for AddStoreShoppingCart was $httpResponse\n",
798
+ Zend_Log::DEBUG,
799
+ 'order_export.log',
800
+ true
801
+ );
802
+
803
+ $AddStorePurchaseResponse = new SimpleXMLElement($httpResponse);
804
+ $response_result = $AddStorePurchaseResponse->Result;
805
+
806
+ if ($response_result == 'Success')
807
+ {
808
+
809
+ Mage::log(
810
+ "Successfully added purchase\n",
811
+ Zend_Log::DEBUG,
812
+ 'order_export.log',
813
+ true
814
+ );
815
+ }
816
+ else
817
+ {
818
+ $response_error = $AddStorePurchaseResponse->ErrorText;
819
+
820
+ Mage::log(
821
+ "Error adding store purchase, error was $response_error\n",
822
+ Zend_Log::DEBUG,
823
+ 'order_export.log',
824
+ true
825
+ );
826
+ }
827
+ }
828
+ else
829
+ {
830
+ $response_error = $AddStoreShoppingCartResponse->ErrorText;
831
+
832
+ Mage::log(
833
+ "Error adding shopping cart, error was $response_error\n",
834
+ Zend_Log::DEBUG,
835
+ 'order_export.log',
836
+ true
837
+ );
838
+ }
839
+ }
840
+ else
841
+ {
842
+ Mage::log(
843
+ "No items found in cart\n",
844
+ Zend_Log::DEBUG,
845
+ 'order_export.log',
846
+ true
847
+ );
848
+ }
849
+ }
850
+ else
851
+ {
852
+ Mage::log(
853
+ "Unrecognized XML\n",
854
+ Zend_Log::DEBUG,
855
+ 'order_export.log',
856
+ true
857
+ );
858
+ }
859
+
860
+ return true;
861
+ }
862
+
863
+ private function Send_CE_XML_Request($request)
864
+ {
865
+ $gr_url = "https://api.stgi.net/xml.pl";
866
+
867
+ $ch = curl_init();
868
+ curl_setopt($ch, CURLOPT_URL, $gr_url);
869
+ curl_setopt($ch, CURLOPT_VERBOSE, 1);
870
+
871
+ // turning off the server and peer verification(TrustManager Concept).
872
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
873
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
874
+
875
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
876
+ curl_setopt($ch, CURLOPT_POST, 1);
877
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
878
+
879
+ return curl_exec($ch);
880
+ }
881
+ }
app/code/community/GreenRope/OrderExport/Model/Observer.php ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class GreenRope_OrderExport_Model_Observer
3
+ {
4
+ /**
5
+ * Exports an order after it is placed
6
+ *
7
+ * @param Varien_Event_Observer $observer observer object
8
+ *
9
+ * @return boolean
10
+ */
11
+ public function exportOrder(Varien_Event_Observer $observer)
12
+ {
13
+ $order = $observer->getEvent()->getOrder();
14
+
15
+ /* Mage::log(
16
+ $order->debug(), //Objects extending Varien_Object can use this
17
+ Zend_Log::DEBUG, //Log level
18
+ 'order_export.log',//Log file name; if blank, will use config value (system.log by default)
19
+ true //force logging regardless of config setting
20
+ );
21
+ */
22
+
23
+ // $dirPath = Mage::getBaseDir('var') . DS . 'export';
24
+
25
+ // file_put_contents($dirPath. DS .$order->getIncrementId().'.txt', var_export($order, 1));
26
+
27
+ Mage::getModel('greenrope_orderexport/export')->exportOrder($order);
28
+ return true;
29
+
30
+ }
31
+ }
32
+ ?>
app/code/community/GreenRope/OrderExport/etc/config.xml ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <modules>
4
+ <GreenRope_OrderExport>
5
+ <version>0.9.0</version>
6
+ </GreenRope_OrderExport>
7
+ </modules>
8
+ <global>
9
+ <helpers>
10
+ <orderexport>
11
+ <class>GreenRope_OrderExport_Helper</class>
12
+ </orderexport>
13
+ </helpers>
14
+ <models>
15
+ <greenrope_orderexport>
16
+ <class>GreenRope_OrderExport_Model</class>
17
+ </greenrope_orderexport>
18
+ </models>
19
+ <events>
20
+ <sales_order_place_after>
21
+ <observers>
22
+ <greenrope_orderexport>
23
+ <class>greenrope_orderexport/observer</class>
24
+ <method>exportOrder</method>
25
+ </greenrope_orderexport>
26
+ </observers>
27
+ </sales_order_place_after>
28
+ </events>
29
+ </global>
30
+ <adminhtml>
31
+ <acl>
32
+ <resources>
33
+ <admin>
34
+ <children>
35
+ <system>
36
+ <children>
37
+ <config>
38
+ <children>
39
+ <greenrope translate="title" module="orderexport">
40
+ <title>GreenRope Order Export Settings</title>
41
+ </greenrope>
42
+ </children>
43
+ </config>
44
+ </children>
45
+ </system>
46
+ </children>
47
+ </admin>
48
+ </resources>
49
+ </acl>
50
+ </adminhtml>
51
+ </config>
52
+
app/code/community/GreenRope/OrderExport/etc/system.xml ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <sections>
4
+ <greenrope translate="label" module="orderexport">
5
+ <label>GreenRope</label>
6
+ <tab>sales</tab>
7
+ <frontend_type>text</frontend_type>
8
+ <sort_order>980</sort_order>
9
+ <show_in_default>1</show_in_default>
10
+ <show_in_website>1</show_in_website>
11
+ <show_in_store>1</show_in_store>
12
+ <groups>
13
+ <settings translate="label">
14
+ <label>GreenRope Order Export Settings</label>
15
+ <frontend_type>text</frontend_type>
16
+ <sort_order>1</sort_order>
17
+ <show_in_default>1</show_in_default>
18
+ <show_in_website>1</show_in_website>
19
+ <show_in_store>1</show_in_store>
20
+ <fields>
21
+ <GreenRopeUsername translate="label comment">
22
+ <label>GreenRope User Name</label>
23
+ <comment>
24
+ Enter the user name for the GreenRope account you would like to export Magento data to.
25
+ </comment>
26
+ <frontend_type>text</frontend_type>
27
+ <sort_order>1</sort_order>
28
+ <show_in_default>1</show_in_default>
29
+ <show_in_website>1</show_in_website>
30
+ <show_in_store>1</show_in_store>
31
+ </GreenRopeUsername>
32
+ <GreenRopePassword translate="label comment">
33
+ <label>GreenRope Password</label>
34
+ <comment>
35
+ Enter the password for the GreenRope account you would like to export Magento data to.
36
+ </comment>
37
+ <frontend_type>password</frontend_type>
38
+ <sort_order>2</sort_order>
39
+ <show_in_default>1</show_in_default>
40
+ <show_in_website>1</show_in_website>
41
+ <show_in_store>1</show_in_store>
42
+ </GreenRopePassword>
43
+ <GreenRopeGroup translate="label comment">
44
+ <label>GreenRope Group</label>
45
+ <comment>
46
+ Enter the group name for the GreenRope group you would like to export Magento data to.
47
+ </comment>
48
+ <frontend_type>text</frontend_type>
49
+ <sort_order>3</sort_order>
50
+ <show_in_default>1</show_in_default>
51
+ <show_in_website>1</show_in_website>
52
+ <show_in_store>1</show_in_store>
53
+ </GreenRopeGroup>
54
+ </fields>
55
+ </settings>
56
+ </groups>
57
+ </greenrope>
58
+ </sections>
59
+ </config>
60
+
app/etc/modules/GreenRope_OrderExport.xml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <GreenRope_OrderExport>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ </GreenRope_OrderExport>
8
+ </modules>
9
+ </config>
package.xml ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>GreenRope_OrderExport</name>
4
+ <version>1.0.0.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://opensource.org/licenses/osl-3.0.php">Open Software License (OSL)</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>This extension sends order information, customer, and item information to the GreenRope CRM software.</summary>
10
+ <description>Do you use Magento for your ecommerce? If so, take your ecommerce even further with the new GreenRope-Magento integration. We have now made it even easier to streamline your sales and marketing efforts. &#xD;
11
+ &#xD;
12
+ Feature Highlights and User Benefits:&#xD;
13
+ &#xD;
14
+ * Import Magento store items and item categories into GreenRope&#xD;
15
+ &#xD;
16
+ * Import Magento customers into GreenRope&#xD;
17
+ &#xD;
18
+ * Import Magento orders to GreenRope&#xD;
19
+ &#xD;
20
+ * Configure Magento to send items, item categories, customer data and order data to GreenRope when subsequent orders are taken from Magento&#xD;
21
+ &#xD;
22
+ Orders can trigger workflows and events in GreenRope such as emails and drip campaigns&#xD;
23
+ &#xD;
24
+ The ability to trigger email campaigns increases efficiency and productivity, while greatly enhancing the customer experience. Now ecommerce businesses using Magento and GreenRope can streamline their follow up, as well as track their customers orders within the CRM. This allows you to, again, gain a more holistic view of the customer, their needs, and ensures more personalized follow up and future engagements. &#xD;
25
+ </description>
26
+ <notes>You need to have a GreenRope account to make use of this extension. You may sign up for one at http://www.greenrope.com.</notes>
27
+ <authors><author><name>Daniel Watkins</name><user>dwatkins1234</user><email>dwatkins@wwzard.com</email></author></authors>
28
+ <date>2014-03-21</date>
29
+ <time>20:22:48</time>
30
+ <contents><target name="mageetc"><dir name="modules"><file name="GreenRope_OrderExport.xml" hash="6b5be39de1715e3f6cac19f1ecfdbc8d"/></dir></target><target name="magecommunity"><dir name="GreenRope"><dir name="OrderExport"><dir name="Helper"><file name="Data.php" hash="cb219a195eb69d4987b6b99e2e369752"/></dir><dir name="Model"><file name="Export.php" hash="8e6c69f4bdcb0b44d2995a4ed0c80728"/><file name="Observer.php" hash="7667a35ce56b20af30375d94d3606ab1"/></dir><dir name="etc"><file name="config.xml" hash="bde89d19793c7741e144e40ebfd4704a"/><file name="system.xml" hash="6bc27ef8f7310d811a81a71b60926ba0"/></dir></dir></dir></target></contents>
31
+ <compatible/>
32
+ <dependencies><required><php><min>5.0.0</min><max>6.0.0</max></php></required></dependencies>
33
+ </package>