Camiloo_Channelunity - Version 1.0.0.10

Version Notes

The ChannelUnity connector kit for Magento.
Fixes an issue which can prevent the deleting of categories. Adds an option to allow ignoring of qty on FBA orders.

Download this release

Release Info

Developer Magento Core Team
Extension Camiloo_Channelunity
Version 1.0.0.10
Comparing to
See all releases


Code changes from version 1.0.0.9 to 1.0.0.10

Files changed (27) hide show
  1. app/code/community/Camiloo/Channelunity/Block/Configheader.php +5 -2
  2. app/code/community/Camiloo/Channelunity/Block/Paymentform.php +3 -3
  3. app/code/community/Camiloo/Channelunity/Block/Paymentinfo.php +5 -3
  4. app/code/community/Camiloo/Channelunity/Helper/Data.php +3 -2
  5. app/code/community/Camiloo/Channelunity/Model/Abstract.php +185 -191
  6. app/code/community/Camiloo/Channelunity/Model/Attributes.php +13 -11
  7. app/code/community/Camiloo/Channelunity/Model/Categories.php +73 -81
  8. app/code/community/Camiloo/Channelunity/Model/Checkforupdates.php +6 -5
  9. app/code/community/Camiloo/Channelunity/Model/Collection.php +8 -4
  10. app/code/community/Camiloo/Channelunity/Model/Customrate.php +37 -37
  11. app/code/community/Camiloo/Channelunity/Model/Entity.php +5 -6
  12. app/code/community/Camiloo/Channelunity/Model/Observer.php +292 -227
  13. app/code/community/Camiloo/Channelunity/Model/Ordercreatebackport.php +12 -9
  14. app/code/community/Camiloo/Channelunity/Model/Orders.php +625 -499
  15. app/code/community/Camiloo/Channelunity/Model/Payment.php +26 -22
  16. app/code/community/Camiloo/Channelunity/Model/Paymentinfo.php +3 -3
  17. app/code/community/Camiloo/Channelunity/Model/Paymentmethoduk.php +8 -6
  18. app/code/community/Camiloo/Channelunity/Model/Products.php +214 -186
  19. app/code/community/Camiloo/Channelunity/Model/Stores.php +52 -53
  20. app/code/community/Camiloo/Channelunity/controllers/ApiController.php +62 -66
  21. app/code/community/Camiloo/Channelunity/etc/config.xml +80 -100
  22. app/code/community/Camiloo/Channelunity/etc/system.xml +45 -24
  23. app/code/community/Camiloo/Channelunity/sql/channelunity_setup/install-1.0.0.php +13 -14
  24. app/code/community/Camiloo/Channelunity/sql/channelunity_setup/mysql4-install-0.0.1.php +2 -3
  25. app/design/adminhtml/default/default/template/channelunity/configheader.phtml +22 -23
  26. app/design/adminhtml/default/default/template/channelunity/paymentinfo.phtml +10 -12
  27. package.xml +5 -5
app/code/community/Camiloo/Channelunity/Block/Configheader.php CHANGED
@@ -1,20 +1,23 @@
1
<?php
2
/**
3
* Camiloo Limited
4
*
5
* NOTICE OF LICENSE
6
-
7
* @category Camiloo
8
* @package Camiloo_Channelunity
9
* @copyright Copyright (c) 2011 Camiloo Limited (http://www.camiloo.co.uk)
10
* @license http://www.camiloo.co.uk/license.txt
11
*/
12
-
13
class Camiloo_Channelunity_Block_Configheader extends Mage_Adminhtml_Block_Abstract implements Varien_Data_Form_Element_Renderer_Interface
14
{
15
protected $_template = 'channelunity/configheader.phtml';
16
public function render(Varien_Data_Form_Element_Abstract $element)
17
{
18
return $this->toHtml();
19
}
20
}
1
<?php
2
+
3
/**
4
* Camiloo Limited
5
*
6
* NOTICE OF LICENSE
7
+
8
* @category Camiloo
9
* @package Camiloo_Channelunity
10
* @copyright Copyright (c) 2011 Camiloo Limited (http://www.camiloo.co.uk)
11
* @license http://www.camiloo.co.uk/license.txt
12
*/
13
class Camiloo_Channelunity_Block_Configheader extends Mage_Adminhtml_Block_Abstract implements Varien_Data_Form_Element_Renderer_Interface
14
{
15
+
16
protected $_template = 'channelunity/configheader.phtml';
17
+
18
public function render(Varien_Data_Form_Element_Abstract $element)
19
{
20
return $this->toHtml();
21
}
22
+
23
}
app/code/community/Camiloo/Channelunity/Block/Paymentform.php CHANGED
@@ -1,20 +1,20 @@
1
<?php
2
/**
3
- * ChannelUnity connector for Magento Commerce
4
*
5
* @category Camiloo
6
* @package Camiloo_Channelunity
7
* @copyright Copyright (c) 2011 Camiloo Limited (http://www.camiloo.co.uk)
8
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9
*/
10
-
11
class Camiloo_Channelunity_Block_Paymentform extends Mage_Payment_Block_Form
12
{
13
14
protected function _construct()
15
{
16
parent::_construct();
17
- // the below will never be visible, but its necessary to have a form block.
18
$this->setTemplate('channelunity/paymentform.phtml');
19
}
20
1
<?php
2
+
3
/**
4
+ * ChannelUnity connector for Magento Commerce
5
*
6
* @category Camiloo
7
* @package Camiloo_Channelunity
8
* @copyright Copyright (c) 2011 Camiloo Limited (http://www.camiloo.co.uk)
9
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
10
*/
11
class Camiloo_Channelunity_Block_Paymentform extends Mage_Payment_Block_Form
12
{
13
14
protected function _construct()
15
{
16
parent::_construct();
17
+ // the below will never be visible, but its necessary to have a form block.
18
$this->setTemplate('channelunity/paymentform.phtml');
19
}
20
app/code/community/Camiloo/Channelunity/Block/Paymentinfo.php CHANGED
@@ -1,6 +1,7 @@
1
<?php
2
/**
3
- * ChannelUnity connector for Magento Commerce
4
*
5
* @category Camiloo
6
* @package Camiloo_Channelunity
@@ -9,6 +10,7 @@
9
*/
10
class Camiloo_Channelunity_Block_Paymentinfo extends Mage_Payment_Block_Info_Cc
11
{
12
/**
13
* Internal constructor
14
*
@@ -17,8 +19,8 @@ class Camiloo_Channelunity_Block_Paymentinfo extends Mage_Payment_Block_Info_Cc
17
protected function _construct()
18
{
19
parent::_construct();
20
-
21
- // Set default template
22
$this->setTemplate('channelunity/paymentinfo.phtml');
23
}
24
1
<?php
2
+
3
/**
4
+ * ChannelUnity connector for Magento Commerce
5
*
6
* @category Camiloo
7
* @package Camiloo_Channelunity
10
*/
11
class Camiloo_Channelunity_Block_Paymentinfo extends Mage_Payment_Block_Info_Cc
12
{
13
+
14
/**
15
* Internal constructor
16
*
19
protected function _construct()
20
{
21
parent::_construct();
22
+
23
+ // Set default template
24
$this->setTemplate('channelunity/paymentinfo.phtml');
25
}
26
app/code/community/Camiloo/Channelunity/Helper/Data.php CHANGED
@@ -1,6 +1,7 @@
1
<?php
2
/**
3
- * ChannelUnity connector for Magento Commerce
4
*
5
* @category Camiloo
6
* @package Camiloo_Channelunity
@@ -9,5 +10,5 @@
9
*/
10
class Camiloo_Channelunity_Helper_Data extends Mage_Core_Helper_Data
11
{
12
-
13
}
1
<?php
2
+
3
/**
4
+ * ChannelUnity connector for Magento Commerce
5
*
6
* @category Camiloo
7
* @package Camiloo_Channelunity
10
*/
11
class Camiloo_Channelunity_Helper_Data extends Mage_Core_Helper_Data
12
{
13
+
14
}
app/code/community/Camiloo/Channelunity/Model/Abstract.php CHANGED
@@ -1,6 +1,7 @@
1
<?php
2
/**
3
- * ChannelUnity connector for Magento Commerce
4
*
5
* @category Camiloo
6
* @package Camiloo_Channelunity
@@ -9,112 +10,110 @@
9
*/
10
class Camiloo_Channelunity_Model_Abstract
11
{
12
-
13
- public function filterCollection($collection, $dataRange){
14
-
15
- foreach($dataRange as $filter){
16
-
17
- $attribute = $this->getAttribute($filter,'on');
18
- $conditions = array();
19
- foreach($filter->operator as $operator){
20
-
21
- $operator = $this->getAttribute($operator,'type');
22
-
23
- foreach($operator->value as $value){
24
- $values[] = (string) $value;
25
- }
26
-
27
- // check if there are multiple values in our array. If not, implode to a string.
28
- if(sizeof($values) < 2){
29
- $values = implode("",$values);
30
- }
31
-
32
- $conditions[$operator] = $values;
33
-
34
-
35
- }
36
-
37
- $collection->addFieldToFilter($attribute,$conditions);
38
-
39
- }
40
-
41
- return $collection;
42
-
43
- }
44
-
45
- private function getAttribute($object, $attribute){
46
-
47
- if(isset($object[$attribute])) {
48
- return (string) $object[$attribute];
49
- }
50
-
51
- }
52
-
53
- public function doRead($dataRange){
54
-
55
- $collection = $this->filterCollection(Mage::getModel($this->_collection)->getCollection(),$dataRange);
56
- $collection->addAttributeToSelect("*");
57
- return $collection->toXml();
58
-
59
- }
60
-
61
- public function doDelete($dataRange){
62
-
63
- $collection = $this->filterCollection(Mage::getModel($this->_collection)->getCollection(),$dataRange);
64
- $deleted_entity_ids = array();
65
- $deleted_increment_ids = array();
66
-
67
- foreach($collection as $item){
68
- $deleted_entity_ids[] = $item->getId();
69
- $deleted_increment_ids[] = $item->getIncrementId();
70
- $item->delete();
71
- }
72
-
73
- $xml = '<?xml version="1.0" encoding="utf-8" ?>';
74
- $xml.= ' <Response>';
75
- $xml.= ' <ResponseHeader>';
76
- $xml.= ' <Status>200</Status>';
77
- $xml.= ' <StatusMessage>Orders deleted successfully</StatusMessage>';
78
- $xml.= ' </ResponseHeader>';
79
- $xml.= ' <ResponseBody>';
80
- foreach($deleted_entity_ids as $key=>$id){
81
- $xml.= ' <DeletedItem>';
82
- $xml.= ' <EntityId>'.$deleted_entity_ids[$key].'</EntityId>';
83
- $xml.= ' <IncrementId>'.$deleted_increment_ids[$key].'</IncrementId>';
84
- $xml.= ' </DeletedItem>';
85
- }
86
- $xml.= ' </ResponseBody>';
87
- $xml.= ' </Response>';
88
-
89
- return $xml;
90
-
91
- }
92
-
93
- private function terminate($message) {
94
95
echo '<?xml version="1.0" encoding="utf-8" ?>';
96
echo ' <ChannelUnity>';
97
- echo ' <Status>'.$message.'</Status>';
98
echo ' </ChannelUnity>';
99
die;
100
}
101
102
- public function getEndpoint() {
103
if (strpos($_SERVER['SERVER_NAME'], "camiloo.co.uk") !== false) {
104
-
105
return "http://staging.channelunity.com/event.php";
106
- }
107
- else {
108
-
109
return "http://my.channelunity.com/event.php";
110
}
111
}
112
113
-
114
/**
115
- * Calls the VerifyNotification API.
116
- */
117
- public function verifypost($messageverify) {
118
119
$session = curl_init();
120
@@ -122,7 +121,7 @@ class Camiloo_Channelunity_Model_Abstract
122
<ChannelUnity>
123
<MerchantName>" . $this->getMerchantName() . "</MerchantName>
124
<Authorization>" . $this->getValidUserAuth() . "</Authorization>
125
- <ApiKey>" .$this->getApiKey(). "</ApiKey>
126
<RequestType>VerifyNotification</RequestType>
127
<Payload>$messageverify</Payload>
128
</ChannelUnity>");
@@ -131,110 +130,106 @@ class Camiloo_Channelunity_Model_Abstract
131
curl_setopt($session, CURLOPT_POST, TRUE);
132
curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);
133
curl_setopt($session, CURLOPT_POSTFIELDS, array('message' => $xml));
134
-
135
- $result = curl_exec($session);
136
137
- try
138
- {
139
- $xml = simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
140
- }
141
- catch (Exception $e)
142
- {
143
- $this->terminate('Error - Unknown response from validation server '.$e);
144
145
}
146
curl_close($session);
147
148
- if ((string) $xml->Status != "OK"){
149
- $this->terminate($xml->Status);
150
-
151
}
152
- else
153
- {
154
- return $xml;
155
}
156
}
157
158
- public function verifyMyself($request) {
159
160
$result = $this->postToChannelUnity("", "ValidateUser");
161
162
if (strpos($result, "<MerchantName>")
163
- || strpos($result, "<merchantname>"))
164
- {
165
echo "<Status>OK</Status>\n";
166
- }
167
- else {
168
169
$xml = simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
170
171
if (isset($xml->Status)) {
172
echo "<Status>{$xml->Status}</Status>\n";
173
- }
174
- else if (isset($xml->status)) {
175
echo "<Status>{$xml->status}</Status>\n";
176
- }
177
- else {
178
echo "<Status>Error - unexpected response</Status>";
179
}
180
}
181
}
182
183
- public function getMerchantName() {
184
return Mage::getStoreConfig('channelunityint/generalsettings/merchantname');
185
}
186
187
- public function getValidUserAuth() {
188
$auth = Mage::getStoreConfig('channelunityint/generalsettings/merchantusername')
189
- . ":" . hash("sha256", Mage::getStoreConfig('channelunityint/generalsettings/merchantpassword'));
190
191
$auth = base64_encode($auth);
192
return $auth;
193
}
194
195
- public function getApiKey() {
196
$apikeyTemp = Mage::getStoreConfig('channelunityint/generalsettings/apikey');
197
198
if (strlen($apikeyTemp) > 0) {
199
return $apikeyTemp;
200
-
201
} else {
202
-
203
$session = curl_init();
204
-
205
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
206
<ChannelUnity>
207
<MerchantName>" . $this->getMerchantName() . "</MerchantName>
208
<Authorization>" . $this->getValidUserAuth() . "</Authorization>
209
<RequestType>ValidateUser</RequestType>
210
</ChannelUnity>";
211
-
212
$xml = urlencode($xml);
213
-
214
curl_setopt($session, CURLOPT_URL, $this->getEndpoint());
215
curl_setopt($session, CURLOPT_POST, TRUE);
216
curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);
217
curl_setopt($session, CURLOPT_POSTFIELDS, array('message' => $xml));
218
-
219
$result = curl_exec($session);
220
-
221
curl_close($session);
222
-
223
- $xml = simplexml_load_string($result, 'SimpleXMLElement',
224
- LIBXML_NOCDATA);
225
226
if (isset($xml->ApiKey)) {
227
Mage::getModel('core/config')->saveConfig(
228
- 'channelunityint/generalsettings/apikey', $xml->ApiKey);
229
-
230
return $xml->ApiKey;
231
}
232
}
233
-
234
return "";
235
}
236
237
- public function postToChannelUnity($xml, $requestType) {
238
239
$session = curl_init();
240
@@ -242,7 +237,7 @@ class Camiloo_Channelunity_Model_Abstract
242
<ChannelUnity>
243
<MerchantName>" . $this->getMerchantName() . "</MerchantName>
244
<Authorization>" . $this->getValidUserAuth() . "</Authorization>
245
- <ApiKey>" .$this->getApiKey(). "</ApiKey>
246
<RequestType>$requestType</RequestType>
247
<Payload>$xml</Payload>
248
</ChannelUnity>";
@@ -258,13 +253,14 @@ class Camiloo_Channelunity_Model_Abstract
258
259
curl_close($session);
260
261
- return $result;
262
}
263
264
- public function postMyURLToChannelUnity($merchantName) {
265
266
$session = curl_init();
267
-
268
$baseurl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
269
270
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
@@ -286,55 +282,53 @@ class Camiloo_Channelunity_Model_Abstract
286
287
curl_close($session);
288
289
- return $result;
290
}
291
-
292
-
293
- /**
294
- * skipProduct - checks whether to skip product to pass it to CU
295
- *
296
- * @param type $product - can be product id or product object
297
- *
298
- * @return boolean - true-skip, false-don't skip
299
- */
300
- public function skipProduct($product)
301
- {
302
- $productStatus = 1;
303
-
304
- $ignoreDisabled = Mage::getStoreConfig('channelunityint/generalsettings/ignoredisabledproducts');
305
-
306
- if($product && $ignoreDisabled == 1)
307
- {
308
- if(is_int($product)) {
309
- $product = Mage::getModel('catalog/product')->load($product);
310
- }
311
-
312
- if(is_object($product) && $product->hasSku()) {
313
- $productStatus = $product->getStatus(); // 1-Enabled, 2-Disabled
314
- }
315
-
316
- if($productStatus == 2) {
317
- return true;
318
- }
319
- }
320
-
321
- return false;
322
- }
323
-
324
- /**
325
- * skipProduct - checks whether to skip product to pass it to CU
326
- *
327
- * //product field: status, 1-Enabled, 2-Disabled
328
- *
329
- * @return boolean - true-ignore disabled, false-don't ignore
330
- */
331
- public function ignoreDisabled()
332
- {
333
- $ignoreDisabled = false;
334
-
335
- $ignoreDisabled = Mage::getStoreConfig('channelunityint/generalsettings/ignoredisabledproducts');
336
-
337
- return $ignoreDisabled;
338
- }
339
- }
340
- ?>
1
<?php
2
+
3
/**
4
+ * ChannelUnity connector for Magento Commerce
5
*
6
* @category Camiloo
7
* @package Camiloo_Channelunity
10
*/
11
class Camiloo_Channelunity_Model_Abstract
12
{
13
+
14
+ public function filterCollection($collection, $dataRange)
15
+ {
16
+
17
+ foreach ($dataRange as $filter) {
18
+
19
+ $attribute = $this->getAttribute($filter, 'on');
20
+ $conditions = array();
21
+ foreach ($filter->operator as $operator) {
22
+
23
+ $operator = $this->getAttribute($operator, 'type');
24
+
25
+ foreach ($operator->value as $value) {
26
+ $values[] = (string) $value;
27
+ }
28
+
29
+ // check if there are multiple values in our array. If not, implode to a string.
30
+ if (sizeof($values) < 2) {
31
+ $values = implode("", $values);
32
+ }
33
+
34
+ $conditions[$operator] = $values;
35
+ }
36
+
37
+ $collection->addFieldToFilter($attribute, $conditions);
38
+ }
39
+
40
+ return $collection;
41
+ }
42
+
43
+ private function getAttribute($object, $attribute)
44
+ {
45
+
46
+ if (isset($object[$attribute])) {
47
+ return (string) $object[$attribute];
48
+ }
49
+ }
50
+
51
+ public function doRead($dataRange)
52
+ {
53
+
54
+ $collection = $this->filterCollection(Mage::getModel($this->_collection)->getCollection(), $dataRange);
55
+ $collection->addAttributeToSelect("*");
56
+ return $collection->toXml();
57
+ }
58
+
59
+ public function doDelete($dataRange)
60
+ {
61
+
62
+ $collection = $this->filterCollection(Mage::getModel($this->_collection)->getCollection(), $dataRange);
63
+ $deleted_entity_ids = array();
64
+ $deleted_increment_ids = array();
65
+
66
+ foreach ($collection as $item) {
67
+ $deleted_entity_ids[] = $item->getId();
68
+ $deleted_increment_ids[] = $item->getIncrementId();
69
+ $item->delete();
70
+ }
71
+
72
+ $xml = '<?xml version="1.0" encoding="utf-8" ?>';
73
+ $xml.= ' <Response>';
74
+ $xml.= ' <ResponseHeader>';
75
+ $xml.= ' <Status>200</Status>';
76
+ $xml.= ' <StatusMessage>Orders deleted successfully</StatusMessage>';
77
+ $xml.= ' </ResponseHeader>';
78
+ $xml.= ' <ResponseBody>';
79
+ foreach ($deleted_entity_ids as $key => $id) {
80
+ $xml.= ' <DeletedItem>';
81
+ $xml.= ' <EntityId>' . $deleted_entity_ids[$key] . '</EntityId>';
82
+ $xml.= ' <IncrementId>' . $deleted_increment_ids[$key] . '</IncrementId>';
83
+ $xml.= ' </DeletedItem>';
84
+ }
85
+ $xml.= ' </ResponseBody>';
86
+ $xml.= ' </Response>';
87
+
88
+ return $xml;
89
+ }
90
+
91
+ private function terminate($message)
92
+ {
93
94
echo '<?xml version="1.0" encoding="utf-8" ?>';
95
echo ' <ChannelUnity>';
96
+ echo ' <Status>' . $message . '</Status>';
97
echo ' </ChannelUnity>';
98
die;
99
}
100
101
+ public function getEndpoint()
102
+ {
103
if (strpos($_SERVER['SERVER_NAME'], "camiloo.co.uk") !== false) {
104
+
105
return "http://staging.channelunity.com/event.php";
106
+ } else {
107
+
108
return "http://my.channelunity.com/event.php";
109
}
110
}
111
112
/**
113
+ * Calls the VerifyNotification API.
114
+ */
115
+ public function verifypost($messageverify)
116
+ {
117
118
$session = curl_init();
119
121
<ChannelUnity>
122
<MerchantName>" . $this->getMerchantName() . "</MerchantName>
123
<Authorization>" . $this->getValidUserAuth() . "</Authorization>
124
+ <ApiKey>" . $this->getApiKey() . "</ApiKey>
125
<RequestType>VerifyNotification</RequestType>
126
<Payload>$messageverify</Payload>
127
</ChannelUnity>");
130
curl_setopt($session, CURLOPT_POST, TRUE);
131
curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);
132
curl_setopt($session, CURLOPT_POSTFIELDS, array('message' => $xml));
133
134
+ $result = curl_exec($session);
135
136
+ try {
137
+ $xmlResult = simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
138
+ } catch (Exception $e) {
139
+ $this->terminate('Error - Unknown response from validation server ' . $e);
140
}
141
curl_close($session);
142
143
+ if (!is_object($xmlResult)) {
144
+ $this->terminate('Error - Result XML was not loaded in verifypost()');
145
}
146
+
147
+ if ((string) $xmlResult->Status != "OK") {
148
+ $this->terminate($xmlResult->Status);
149
+ } else {
150
+ return $xmlResult;
151
}
152
}
153
154
+ public function verifyMyself($request)
155
+ {
156
157
$result = $this->postToChannelUnity("", "ValidateUser");
158
159
if (strpos($result, "<MerchantName>")
160
+ || strpos($result, "<merchantname>")) {
161
echo "<Status>OK</Status>\n";
162
+ } else {
163
164
$xml = simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
165
166
if (isset($xml->Status)) {
167
echo "<Status>{$xml->Status}</Status>\n";
168
+ } else if (isset($xml->status)) {
169
echo "<Status>{$xml->status}</Status>\n";
170
+ } else {
171
echo "<Status>Error - unexpected response</Status>";
172
}
173
}
174
}
175
176
+ public function getMerchantName()
177
+ {
178
return Mage::getStoreConfig('channelunityint/generalsettings/merchantname');
179
}
180
181
+ public function getValidUserAuth()
182
+ {
183
$auth = Mage::getStoreConfig('channelunityint/generalsettings/merchantusername')
184
+ . ":" . hash("sha256", Mage::getStoreConfig('channelunityint/generalsettings/merchantpassword'));
185
186
$auth = base64_encode($auth);
187
return $auth;
188
}
189
190
+ public function getApiKey()
191
+ {
192
$apikeyTemp = Mage::getStoreConfig('channelunityint/generalsettings/apikey');
193
194
if (strlen($apikeyTemp) > 0) {
195
return $apikeyTemp;
196
} else {
197
+
198
$session = curl_init();
199
+
200
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
201
<ChannelUnity>
202
<MerchantName>" . $this->getMerchantName() . "</MerchantName>
203
<Authorization>" . $this->getValidUserAuth() . "</Authorization>
204
<RequestType>ValidateUser</RequestType>
205
</ChannelUnity>";
206
+
207
$xml = urlencode($xml);
208
+
209
curl_setopt($session, CURLOPT_URL, $this->getEndpoint());
210
curl_setopt($session, CURLOPT_POST, TRUE);
211
curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);
212
curl_setopt($session, CURLOPT_POSTFIELDS, array('message' => $xml));
213
+
214
$result = curl_exec($session);
215
+
216
curl_close($session);
217
+
218
+ $xml = simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
219
220
if (isset($xml->ApiKey)) {
221
Mage::getModel('core/config')->saveConfig(
222
+ 'channelunityint/generalsettings/apikey', $xml->ApiKey);
223
+
224
return $xml->ApiKey;
225
}
226
}
227
+
228
return "";
229
}
230
231
+ public function postToChannelUnity($xml, $requestType)
232
+ {
233
234
$session = curl_init();
235
237
<ChannelUnity>
238
<MerchantName>" . $this->getMerchantName() . "</MerchantName>
239
<Authorization>" . $this->getValidUserAuth() . "</Authorization>
240
+ <ApiKey>" . $this->getApiKey() . "</ApiKey>
241
<RequestType>$requestType</RequestType>
242
<Payload>$xml</Payload>
243
</ChannelUnity>";
253
254
curl_close($session);
255
256
+ return $result;
257
}
258
259
+ public function postMyURLToChannelUnity($merchantName)
260
+ {
261
262
$session = curl_init();
263
+
264
$baseurl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
265
266
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
282
283
curl_close($session);
284
285
+ return $result;
286
}
287
+
288
+ /**
289
+ * skipProduct - checks whether to skip product to pass it to CU
290
+ *
291
+ * @param type $product - can be product id or product object
292
+ *
293
+ * @return boolean - true-skip, false-don't skip
294
+ */
295
+ public function skipProduct($product)
296
+ {
297
+ $productStatus = 1;
298
+
299
+ $ignoreDisabled = Mage::getStoreConfig('channelunityint/generalsettings/ignoredisabledproducts');
300
+
301
+ if ($product && $ignoreDisabled == 1) {
302
+ if (is_int($product)) {
303
+ $product = Mage::getModel('catalog/product')->load($product);
304
+ }
305
+
306
+ if (is_object($product) && $product->hasSku()) {
307
+ $productStatus = $product->getStatus(); // 1-Enabled, 2-Disabled
308
+ }
309
+
310
+ if ($productStatus == 2) {
311
+ return true;
312
+ }
313
+ }
314
+
315
+ return false;
316
+ }
317
+
318
+ /**
319
+ * skipProduct - checks whether to skip product to pass it to CU
320
+ *
321
+ * //product field: status, 1-Enabled, 2-Disabled
322
+ *
323
+ * @return boolean - true-ignore disabled, false-don't ignore
324
+ */
325
+ public function ignoreDisabled()
326
+ {
327
+ $ignoreDisabled = false;
328
+
329
+ $ignoreDisabled = Mage::getStoreConfig('channelunityint/generalsettings/ignoredisabledproducts');
330
+
331
+ return $ignoreDisabled;
332
+ }
333
+
334
+ }
app/code/community/Camiloo/Channelunity/Model/Attributes.php CHANGED
@@ -1,6 +1,7 @@
1
<?php
2
/**
3
- * ChannelUnity connector for Magento Commerce
4
*
5
* @category Camiloo
6
* @package Camiloo_Channelunity
@@ -10,16 +11,17 @@
10
class Camiloo_Channelunity_Model_Attributes extends Camiloo_Channelunity_Model_Abstract
11
{
12
13
- protected $_collection = 'eav/entity_attribute';
14
-
15
- public function doCreate($dataArray){
16
-
17
- }
18
-
19
- public function doUpdate($dataArray){
20
-
21
- }
22
-
23
24
}
25
1
<?php
2
+
3
/**
4
+ * ChannelUnity connector for Magento Commerce
5
*
6
* @category Camiloo
7
* @package Camiloo_Channelunity
11
class Camiloo_Channelunity_Model_Attributes extends Camiloo_Channelunity_Model_Abstract
12
{
13
14
+ protected $_collection = 'eav/entity_attribute';
15
+
16
+ public function doCreate($dataArray)
17
+ {
18
+
19
+ }
20
+
21
+ public function doUpdate($dataArray)
22
+ {
23
+
24
+ }
25
26
}
27
app/code/community/Camiloo/Channelunity/Model/Categories.php CHANGED
@@ -1,6 +1,7 @@
1
<?php
2
/**
3
- * ChannelUnity connector for Magento Commerce
4
*
5
* @category Camiloo
6
* @package Camiloo_Channelunity
@@ -10,17 +11,18 @@
10
class Camiloo_Channelunity_Model_Categories extends Camiloo_Channelunity_Model_Abstract
11
{
12
13
- protected $_collection = 'catalog/category';
14
-
15
/**
16
* Returns an XML list of all categories in this Magento install.
17
*/
18
- public function doRead($request) {
19
$collection = Mage::getModel($this->_collection)->getCollection()
20
- ->addAttributeToSelect("name");
21
-
22
// position, category_path, parent, level
23
-
24
echo "<CategoryList>\n";
25
foreach ($collection as $category) {
26
echo "<Category>\n";
@@ -32,20 +34,19 @@ class Camiloo_Channelunity_Model_Categories extends Camiloo_Channelunity_Model_A
32
echo " <Level>{$category->getData('level')}</Level>\n";
33
echo "</Category>\n\n";
34
}
35
-
36
echo "</CategoryList>";
37
- }
38
-
39
- public function enumerateCategoriesForStoreView($urlTemp, $frameworkType,
40
- $websiteId, $storeId,
41
- $rootCatId, $storeViewId) {
42
$messageToSend = "";
43
-
44
// Load in this root category and enumerate all children
45
$collection = Mage::getModel($this->_collection)->getCollection()
46
- ->addAttributeToSelect("name");
47
-
48
- // need to be able to link categories up to the right source/store in CU.
49
50
$messageToSend .= "<CategoryList>
51
<URL><![CDATA[{$urlTemp}]]></URL>
@@ -53,98 +54,89 @@ class Camiloo_Channelunity_Model_Categories extends Camiloo_Channelunity_Model_A
53
<WebsiteId><![CDATA[{$websiteId}]]></WebsiteId>
54
<StoreId><![CDATA[{$storeId}]]></StoreId>
55
<StoreviewId><![CDATA[{$storeViewId}]]></StoreviewId>\n";
56
-
57
foreach ($collection as $category) {
58
-
59
- // $children = $category->getChildren();
60
- $pid = $category->getData('parent_id');
61
- $lvl = $category->getData('level');
62
- // $childCount = $children->count();
63
-
64
- $catPathTemp = $category->getData('path');
65
-
66
- if (strpos($catPathTemp, "$rootCatId/") === 0 // start of path
67
- || strpos($catPathTemp, "/$rootCatId/") > 0 // middle of path
68
- || strpos($catPathTemp, "/$rootCatId") == (strlen($catPathTemp)-strlen("/$rootCatId")) ) // OR at END of path
69
- {
70
- $messageToSend .= "<Category>\n";
71
- $messageToSend .= " <ID><![CDATA[{$category->getId()}]]></ID>\n";
72
- $messageToSend .= " <Name><![CDATA[{$category->getName()}]]></Name>\n";
73
- $messageToSend .= " <Position><![CDATA[{$category->getData('position')}]]></Position>\n";
74
- $messageToSend .= " <CategoryPath><![CDATA[{$catPathTemp}]]></CategoryPath>\n";
75
- $messageToSend .= " <ParentID><![CDATA[{$category->getData('parent_id')}]]></ParentID>\n";
76
- $messageToSend .= " <Level><![CDATA[{$category->getData('level')}]]></Level>\n";
77
- $messageToSend .= "</Category>\n\n";
78
-
79
- }
80
-
81
}
82
-
83
$messageToSend .= "</CategoryList>";
84
-
85
return $messageToSend;
86
}
87
-
88
- public function postCategoriesToCU($urlTemp) {
89
$messageToSend = '';
90
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
91
-
92
$websites = Mage::app()->getWebsites();
93
-
94
- $putData = tmpfile();
95
- $bytes = 0;
96
-
97
// For each store view ...
98
foreach ($websites as $website) {
99
-
100
$stores = Mage::getModel('core/store_group')
101
- ->getCollection()->addFieldToFilter('website_id', array('eq' => $website->getData('website_id')));
102
-
103
foreach ($stores as $store) {
104
-
105
// Get the root category ID ...
106
107
$rootCatId = $store->getData('root_category_id');
108
-
109
$storeViews = Mage::getModel('core/store')
110
- ->getCollection()->addFieldToFilter('website_id', array('eq' => $website->getData('website_id')))
111
- ->addFieldToFilter('group_id', array('eq' => $store->getData('group_id')));
112
-
113
foreach ($storeViews as $storeView) {
114
-
115
$frameworkType = "Magento";
116
$websiteId = $storeView->getData('website_id');
117
$storeId = $storeView->getData('group_id');
118
$storeViewId = $storeView->getData('store_id');
119
-
120
- $messageToSend = $this->enumerateCategoriesForStoreView($urlTemp, $frameworkType,
121
- $websiteId, $storeId,
122
- $rootCatId, $storeViewId);
123
-
124
- $bytes = $bytes + fwrite($putData, $messageToSend);
125
-
126
}
127
}
128
}
129
-
130
- fseek($putData, 0);
131
- $senditnow = fread($putData, $bytes);
132
- $result = $this->postToChannelUnity($senditnow, "CategoryData");
133
$xml = simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
134
-
135
- fclose($putData);
136
-
137
if (isset($xml->Status)) {
138
return $xml->Status;
139
- }
140
- else if (isset($xml->status)) {
141
return $xml->status;
142
- }
143
- else {
144
- return "Error - unexpected response";
145
}
146
}
147
148
- }
149
-
150
- ?>
1
<?php
2
+
3
/**
4
+ * ChannelUnity connector for Magento Commerce
5
*
6
* @category Camiloo
7
* @package Camiloo_Channelunity
11
class Camiloo_Channelunity_Model_Categories extends Camiloo_Channelunity_Model_Abstract
12
{
13
14
+ protected $_collection = 'catalog/category';
15
+
16
/**
17
* Returns an XML list of all categories in this Magento install.
18
*/
19
+ public function doRead($request)
20
+ {
21
$collection = Mage::getModel($this->_collection)->getCollection()
22
+ ->addAttributeToSelect("name");
23
+
24
// position, category_path, parent, level
25
+
26
echo "<CategoryList>\n";
27
foreach ($collection as $category) {
28
echo "<Category>\n";
34
echo " <Level>{$category->getData('level')}</Level>\n";
35
echo "</Category>\n\n";
36
}
37
+
38
echo "</CategoryList>";
39
+ }
40
+
41
+ public function enumerateCategoriesForStoreView($urlTemp, $frameworkType, $websiteId, $storeId, $rootCatId, $storeViewId)
42
+ {
43
$messageToSend = "";
44
+
45
// Load in this root category and enumerate all children
46
$collection = Mage::getModel($this->_collection)->getCollection()
47
+ ->addAttributeToSelect("name");
48
+
49
+ // need to be able to link categories up to the right source/store in CU.
50
51
$messageToSend .= "<CategoryList>
52
<URL><![CDATA[{$urlTemp}]]></URL>
54
<WebsiteId><![CDATA[{$websiteId}]]></WebsiteId>
55
<StoreId><![CDATA[{$storeId}]]></StoreId>
56
<StoreviewId><![CDATA[{$storeViewId}]]></StoreviewId>\n";
57
+
58
foreach ($collection as $category) {
59
+
60
+ // $children = $category->getChildren();
61
+ $pid = $category->getData('parent_id');
62
+ $lvl = $category->getData('level');
63
+ // $childCount = $children->count();
64
+
65
+ $catPathTemp = $category->getData('path');
66
+
67
+ if (strpos($catPathTemp, "$rootCatId/") === 0 // start of path
68
+ || strpos($catPathTemp, "/$rootCatId/") > 0 // middle of path
69
+ || strpos($catPathTemp, "/$rootCatId") == (strlen($catPathTemp) - strlen("/$rootCatId"))) { // OR at END of path
70
+ $messageToSend .= "<Category>\n";
71
+ $messageToSend .= " <ID><![CDATA[{$category->getId()}]]></ID>\n";
72
+ $messageToSend .= " <Name><![CDATA[{$category->getName()}]]></Name>\n";
73
+ $messageToSend .= " <Position><![CDATA[{$category->getData('position')}]]></Position>\n";
74
+ $messageToSend .= " <CategoryPath><![CDATA[{$catPathTemp}]]></CategoryPath>\n";
75
+ $messageToSend .= " <ParentID><![CDATA[{$category->getData('parent_id')}]]></ParentID>\n";
76
+ $messageToSend .= " <Level><![CDATA[{$category->getData('level')}]]></Level>\n";
77
+ $messageToSend .= "</Category>\n\n";
78
+ }
79
}
80
+
81
$messageToSend .= "</CategoryList>";
82
+
83
return $messageToSend;
84
}
85
+
86
+ public function postCategoriesToCU($urlTemp)
87
+ {
88
$messageToSend = '';
89
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
90
+
91
$websites = Mage::app()->getWebsites();
92
+
93
+ $putData = tmpfile();
94
+ $bytes = 0;
95
+
96
// For each store view ...
97
foreach ($websites as $website) {
98
+
99
$stores = Mage::getModel('core/store_group')
100
+ ->getCollection()->addFieldToFilter('website_id', array('eq' => $website->getData('website_id')));
101
+
102
foreach ($stores as $store) {
103
+
104
// Get the root category ID ...
105
106
$rootCatId = $store->getData('root_category_id');
107
+
108
$storeViews = Mage::getModel('core/store')
109
+ ->getCollection()->addFieldToFilter('website_id', array('eq' => $website->getData('website_id')))
110
+ ->addFieldToFilter('group_id', array('eq' => $store->getData('group_id')));
111
+
112
foreach ($storeViews as $storeView) {
113
+
114
$frameworkType = "Magento";
115
$websiteId = $storeView->getData('website_id');
116
$storeId = $storeView->getData('group_id');
117
$storeViewId = $storeView->getData('store_id');
118
+
119
+ $messageToSend = $this->enumerateCategoriesForStoreView($urlTemp, $frameworkType, $websiteId, $storeId, $rootCatId, $storeViewId);
120
+
121
+ $bytes = $bytes + fwrite($putData, $messageToSend);
122
}
123
}
124
}
125
+
126
+ fseek($putData, 0);
127
+ $senditnow = fread($putData, $bytes);
128
+ $result = $this->postToChannelUnity($senditnow, "CategoryData");
129
$xml = simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
130
+
131
+ fclose($putData);
132
+
133
if (isset($xml->Status)) {
134
return $xml->Status;
135
+ } else if (isset($xml->status)) {
136
return $xml->status;
137
+ } else {
138
+ return "Error - unexpected response";
139
}
140
}
141
142
+ }
app/code/community/Camiloo/Channelunity/Model/Checkforupdates.php CHANGED
@@ -1,6 +1,7 @@
1
<?php
2
/**
3
- * ChannelUnity connector for Magento Commerce
4
*
5
* @category Camiloo
6
* @package Camiloo_Channelunity
@@ -9,7 +10,9 @@
9
*/
10
class Camiloo_Channelunity_Model_Checkforupdates extends Varien_Object
11
{
12
- public function getRemoteXMLFileData($urltograb){
13
// this function gets the requested data
14
$session = curl_init("$urltograb");
15
curl_setopt($session, CURLOPT_HEADER, false);
@@ -18,11 +21,9 @@ class Camiloo_Channelunity_Model_Checkforupdates extends Varien_Object
18
curl_setopt($session, CURLOPT_TIMEOUT, 60);
19
$result = curl_exec($session);
20
curl_close($session);
21
- return simplexml_load_string($result,'SimpleXMLElement', LIBXML_NOCDATA);
22
}
23
-
24
25
-
26
}
27
28
?>
1
<?php
2
+
3
/**
4
+ * ChannelUnity connector for Magento Commerce
5
*
6
* @category Camiloo
7
* @package Camiloo_Channelunity
10
*/
11
class Camiloo_Channelunity_Model_Checkforupdates extends Varien_Object
12
{
13
+
14
+ public function getRemoteXMLFileData($urltograb)
15
+ {
16
// this function gets the requested data
17
$session = curl_init("$urltograb");
18
curl_setopt($session, CURLOPT_HEADER, false);
21
curl_setopt($session, CURLOPT_TIMEOUT, 60);
22
$result = curl_exec($session);
23
curl_close($session);
24
+ return simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA);
25
}
26
27
}
28
29
?>
app/code/community/Camiloo/Channelunity/Model/Collection.php CHANGED
@@ -1,12 +1,16 @@
1
<?php
2
if (version_compare(Mage::getVersion(), "1.6.0.0", ">=")
3
- && class_exists("Mage_Catalog_Model_Resource_Product_Collection")) {
4
-
5
- class Camiloo_Channelunity_Model_Collection extends Mage_Catalog_Model_Resource_Product_Collection {
6
public function isEnabledFlat()
7
{
8
return false;
9
}
10
}
11
-
12
}
1
<?php
2
+
3
if (version_compare(Mage::getVersion(), "1.6.0.0", ">=")
4
+ && class_exists("Mage_Catalog_Model_Resource_Product_Collection")) {
5
+
6
+ class Camiloo_Channelunity_Model_Collection extends Mage_Catalog_Model_Resource_Product_Collection
7
+ {
8
+
9
public function isEnabledFlat()
10
{
11
return false;
12
}
13
+
14
}
15
+
16
}
app/code/community/Camiloo/Channelunity/Model/Customrate.php CHANGED
@@ -1,61 +1,61 @@
1
<?php
2
/**
3
- * ChannelUnity connector for Magento Commerce
4
*
5
* @category Camiloo
6
* @package Camiloo_Channelunity
7
* @copyright Copyright (c) 2012 Camiloo Limited (http://www.camiloo.co.uk)
8
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9
*/
10
- class Camiloo_Channelunity_Model_Customrate
11
- extends Mage_Shipping_Model_Carrier_Abstract
12
- implements Mage_Shipping_Model_Carrier_Interface
13
{
14
-
15
protected $_code = 'channelunitycustomrate';
16
protected $_isFixed = true;
17
-
18
-
19
- public function isActive() {
20
- return false;
21
- }
22
-
23
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
24
{
25
if (!$this->getConfigFlag('active')) {
26
return false;
27
}
28
-
29
- // check if we are getting the rate for a CU request.
30
- // to do this, the easiest way is to check for some form
31
- // of CU data incoming.
32
- $cuOrderCheck = Mage::registry('cu_order_in_progress');
33
- if($cuOrderCheck == 1){
34
- $result = Mage::getModel('shipping/rate_result');
35
-
36
- $method = Mage::getModel('shipping/rate_result_method');
37
- $method->setCarrier('channelunitycustomrate');
38
- $method->setCarrierTitle($this->getConfigData('title'));
39
- $method->setMethod('channelunitycustomrate');
40
- // $method->setMethodTitle(Mage::getSingleton('core/session')->getShippingMethod());
41
-
42
- $shipPrice = Mage::getSingleton('core/session')->getShippingPrice();
43
-
44
- $method->setPrice($shipPrice);
45
- $method->setCost($shipPrice);
46
-
47
- $result->append($method);
48
-
49
- return $result;
50
- }else{
51
- return false;
52
- }
53
}
54
-
55
public function getAllowedMethods()
56
{
57
return array('channelunitycustomrate' => $this->getConfigData('name'));
58
}
59
}
60
61
?>
1
<?php
2
+
3
/**
4
+ * ChannelUnity connector for Magento Commerce
5
*
6
* @category Camiloo
7
* @package Camiloo_Channelunity
8
* @copyright Copyright (c) 2012 Camiloo Limited (http://www.camiloo.co.uk)
9
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
10
*/
11
+ class Camiloo_Channelunity_Model_Customrate extends Mage_Shipping_Model_Carrier_Abstract implements Mage_Shipping_Model_Carrier_Interface
12
{
13
+
14
protected $_code = 'channelunitycustomrate';
15
protected $_isFixed = true;
16
+
17
+ public function isActive()
18
+ {
19
+ return false;
20
+ }
21
+
22
public function collectRates(Mage_Shipping_Model_Rate_Request $request)
23
{
24
if (!$this->getConfigFlag('active')) {
25
return false;
26
}
27
+
28
+ // check if we are getting the rate for a CU request.
29
+ // to do this, the easiest way is to check for some form
30
+ // of CU data incoming.
31
+ $cuOrderCheck = Mage::registry('cu_order_in_progress');
32
+ if ($cuOrderCheck == 1) {
33
+ $result = Mage::getModel('shipping/rate_result');
34
+
35
+ $method = Mage::getModel('shipping/rate_result_method');
36
+ $method->setCarrier('channelunitycustomrate');
37
+ $method->setCarrierTitle($this->getConfigData('title'));
38
+ $method->setMethod('channelunitycustomrate');
39
+ // $method->setMethodTitle(Mage::getSingleton('core/session')->getShippingMethod());
40
+
41
+ $shipPrice = Mage::getSingleton('core/session')->getShippingPrice();
42
+
43
+ $method->setPrice($shipPrice);
44
+ $method->setCost($shipPrice);
45
+
46
+ $result->append($method);
47
+
48
+ return $result;
49
+ } else {
50
+ return false;
51
+ }
52
}
53
+
54
public function getAllowedMethods()
55
{
56
return array('channelunitycustomrate' => $this->getConfigData('name'));
57
}
58
+
59
}
60
61
?>
app/code/community/Camiloo/Channelunity/Model/Entity.php CHANGED
@@ -1,17 +1,16 @@
1
<?php
2
/**
3
- * ChannelUnity connector for Magento Commerce
4
*
5
* @category Camiloo
6
* @package Camiloo_Channelunity
7
* @copyright Copyright (c) 2012 Camiloo Limited (http://www.camiloo.co.uk)
8
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9
*/
10
- class Camiloo_Channelunity_Model_Entity extends Mage_Eav_Model_Entity_Setup {
11
-
12
-
13
- }
14
-
15
16
17
?>
1
<?php
2
+
3
/**
4
+ * ChannelUnity connector for Magento Commerce
5
*
6
* @category Camiloo
7
* @package Camiloo_Channelunity
8
* @copyright Copyright (c) 2012 Camiloo Limited (http://www.camiloo.co.uk)
9
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
10
*/
11
+ class Camiloo_Channelunity_Model_Entity extends Mage_Eav_Model_Entity_Setup
12
+ {
13
14
+ }
15
16
?>
app/code/community/Camiloo/Channelunity/Model/Observer.php CHANGED
@@ -17,232 +17,211 @@ class Camiloo_Channelunity_Model_Observer extends Camiloo_Channelunity_Model_Abs
17
{
18
19
/**
20
- * Called on saving a product in Magento.
21
*/
22
- public function productWasSaved(Varien_Event_Observer $observer)
23
- {
24
try {
25
- $product = $observer->getEvent()->getProduct();
26
27
- $skipProduct = Mage::getModel('channelunity/products')->skipProduct($product);
28
-
29
- $storeViewId = $product->getStoreId();
30
-
31
- if(!$skipProduct)
32
- {
33
- $xml = "<Products>\n";
34
-
35
- $xml .= "<SourceURL>".Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB)."</SourceURL>\n";
36
37
- $xml .= "<StoreViewId>$storeViewId</StoreViewId>\n";
38
39
- $xml .= Mage::getModel('channelunity/products')->generateCuXmlForSingleProduct($product->getId(), $storeViewId);
40
41
- $xml .= "</Products>\n";
42
43
- $this->postToChannelUnity($xml, "ProductData");
44
- } else {
45
- $xml = "<Products>\n";
46
-
47
- $xml .= "<SourceURL>" . Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB)."</SourceURL>\n";
48
-
49
- $xml .= "<StoreViewId>$storeViewId</StoreViewId>\n";
50
51
- $xml .= "<DeletedProductId>{$product->getId()}</DeletedProductId>\n";
52
53
- $xml .= "</Products>\n";
54
55
- $this->postToChannelUnity($xml, "ProductData");
56
- }
57
- }
58
- catch (Exception $x) {
59
- Mage::logException($e);
60
- }
61
- }
62
63
- /**
64
- * Called on deleting a product in Magento.
65
- */
66
- public function productWasDeleted(Varien_Event_Observer $observer)
67
- {
68
- try {
69
- $product = $observer->getEvent()->getProduct();
70
71
- $storeViewId = $product->getStoreId();
72
73
- $xml = "<Products>\n";
74
-
75
- $xml .= "<SourceURL>" . Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB)
76
- . "</SourceURL>\n";
77
- $xml .= "<StoreViewId>$storeViewId</StoreViewId>\n";
78
-
79
- $xml .= "<DeletedProductId>{$product->getId()}</DeletedProductId>\n";
80
81
- $xml .= "</Products>\n";
82
83
- $this->postToChannelUnity($xml, "ProductData");
84
} catch (Exception $e) {
85
Mage::logException($e);
86
}
87
}
88
89
/**
90
- * Allows the observing of more generic events in Magento.
91
- * Useful in multiple product save for example.
92
*/
93
- public function hookToControllerActionPostDispatch($observer)
94
{
95
try {
96
- $evname = $observer->getEvent()->getControllerAction()->getFullActionName();
97
98
- if ($evname == 'adminhtml_catalog_product_action_attribute_save') {
99
- $xml = "<Products>\n";
100
- $xml .= "<SourceURL>" . Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB)
101
- . "</SourceURL>\n";
102
103
- $storeViewId = Mage::helper('adminhtml/catalog_product_edit_action_attribute')->getSelectedStoreId();
104
- $xml .= "<StoreViewId>$storeViewId</StoreViewId>\n";
105
106
- $pids = Mage::helper('adminhtml/catalog_product_edit_action_attribute')->getProductIds();
107
108
- foreach ($pids as $productId) {
109
- $xml .= Mage::getModel('channelunity/products')->generateCuXmlForSingleProduct(
110
- $productId, $storeViewId);
111
}
112
113
- $xml .= "</Products>\n";
114
-
115
- $this->postToChannelUnity($xml, "ProductData");
116
- } else if ($evname == 'adminhtml_catalog_category_save') {
117
-
118
- $this->categorySave($observer);
119
- } else if ($evname == 'adminhtml_catalog_category_delete') {
120
-
121
- $this->categoryDelete($observer);
122
- } else if ($evname == 'adminhtml_catalog_product_delete') {
123
- $xml = "<Products>\n";
124
- $xml .= "<SourceURL>" . Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB)
125
- . "</SourceURL>\n";
126
-
127
- $storeViewId = Mage::helper('adminhtml/catalog_product_edit_action_attribute')->getSelectedStoreId();
128
- $xml .= "<StoreViewId>$storeViewId</StoreViewId>\n";
129
-
130
- $productId = $observer->getEvent()->getControllerAction()->getRequest()->getParam('id');
131
-
132
- $xml .= "<DeletedProductId>" . $productId . "</DeletedProductId>\n";
133
-
134
- $xml .= "</Products>\n";
135
-
136
- $this->postToChannelUnity($xml, "ProductData");
137
- } else if ($evname == 'adminhtml_catalog_product_massStatus') { //update all products status on the massive status update
138
-
139
- $updatedProductsId = $observer->getEvent()->getControllerAction()->getRequest()->getParam('product');
140
- $status = $observer->getEvent()->getControllerAction()->getRequest()->getParam('status');
141
-
142
- if(is_array($updatedProductsId) && !empty($updatedProductsId))
143
- {
144
- $storeViewId = Mage::helper('adminhtml/catalog_product_edit_action_attribute')->getSelectedStoreId();
145
-
146
- $xml = "<Products>\n";
147
- $xml .= "<SourceURL>" . Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB)."</SourceURL>\n";
148
- $xml .= "<StoreViewId>{$storeViewId}</StoreViewId>\n";
149
-
150
- foreach ($updatedProductsId as $productId)
151
- {
152
- $product = Mage::getModel('catalog/product')->load($productId);
153
-
154
- $skipProduct = Mage::getModel('channelunity/products')->skipProduct($product);
155
-
156
- if($skipProduct)
157
- {
158
- $xml .= "<DeletedProductId>" . $productId . "</DeletedProductId>\n";
159
- } else {
160
- $xml .= Mage::getModel('channelunity/products')->generateCuXmlForSingleProduct($productId, $storeViewId);
161
- }
162
- }
163
-
164
- $xml .= "</Products>\n";
165
-
166
- $this->postToChannelUnity($xml, "ProductData");
167
- }
168
-
169
- }
170
} catch (Exception $e) {
171
Mage::logException($e);
172
}
173
}
174
175
/**
176
- * Called on placing an order. Stock levels are updated on CU.
177
*/
178
- public function orderWasPlaced(Varien_Event_Observer $observer)
179
{
180
try {
181
- if (is_object($observer)) {
182
-
183
- $ev = $observer->getEvent();
184
-
185
- if (is_object($ev)) {
186
-
187
- $order = $ev->getOrder();
188
189
- if (is_object($order)) {
190
191
- $items = $order->getAllItems();
192
193
- $this->getItemsForUpdateCommon($items, $order->getStore()->getId());
194
- }
195
- }
196
- }
197
} catch (Exception $e) {
198
Mage::logException($e);
199
}
200
}
201
202
- public function getItemsForUpdateCommon($items, $storeId)
203
{
204
try {
205
- $xml = "<Products>\n";
206
- $xml .= "<SourceURL>" . Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB)
207
- . "</SourceURL>\n";
208
-
209
- $xml .= "<StoreViewId>$storeId</StoreViewId>\n";
210
-
211
- foreach ($items as $item) {
212
213
- $sku = $item->getSku();
214
-
215
- $prodTemp = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
216
- if (!$prodTemp) {
217
-
218
- continue;
219
- }
220
-
221
- // Item was ordered on website, stock will have reduced, update to CU
222
- $xml .= Mage::getModel('channelunity/products')->generateCuXmlForSingleProduct(
223
- $prodTemp->getId(), $storeId, 0 /* $item->getQtyOrdered() */);
224
-
225
- }
226
- $xml .= "</Products>\n";
227
-
228
- $this->postToChannelUnity($xml, "ProductData");
229
-
230
- } catch (Exception $x) {
231
Mage::logException($e);
232
}
233
}
234
235
public function onInvoicePaid(Varien_Event_Observer $observer)
236
{
237
try {
238
- if (is_object($observer) && is_object($observer->getInvoice())) {
239
- $order = $observer->getInvoice()->getOrder();
240
241
- if (is_object($order)) {
242
- $items = $order->getAllItems();
243
- $this->getItemsForUpdateCommon($items, $order->getStore()->getId());
244
- }
245
- }
246
} catch (Exception $e) {
247
Mage::logException($e);
248
}
@@ -254,10 +233,16 @@ class Camiloo_Channelunity_Model_Observer extends Camiloo_Channelunity_Model_Abs
254
public function checkForCancellation(Varien_Event_Observer $observer)
255
{
256
try {
257
$order = $observer->getOrder();
258
259
- $xml = Mage::getModel('channelunity/orders')->generateCuXmlForOrderStatus($order);
260
- $this->postToChannelUnity($xml, "OrderStatusUpdate");
261
} catch (Exception $e) {
262
Mage::logException($e);
263
}
@@ -269,19 +254,29 @@ class Camiloo_Channelunity_Model_Observer extends Camiloo_Channelunity_Model_Abs
269
public function saveTrackingToAmazon(Varien_Event_Observer $observer)
270
{
271
try {
272
- // Only mark as shipped when order has tracking information.
273
$track = $observer->getEvent()->getTrack();
274
$order = $track->getShipment()->getOrder();
275
276
- if ($track->getCarrierCode() == "custom") {
277
- $carrierName = $track->getTitle();
278
- } else {
279
- $carrierName = $track->getCarrierCode();
280
}
281
282
- $xml = Mage::getModel('channelunity/orders')->generateCuXmlForOrderShip($order, $carrierName, $track->getTitle(), $track->getNumber());
283
- $result = $this->postToChannelUnity($xml, "OrderStatusUpdate");
284
- Mage::log('saveTrackingToAmazon: ' . $result);
285
} catch (Exception $e) {
286
Mage::logException($e);
287
}
@@ -290,14 +285,22 @@ class Camiloo_Channelunity_Model_Observer extends Camiloo_Channelunity_Model_Abs
290
public function shipAmazon(Varien_Event_Observer $observer)
291
{
292
try {
293
$shipment = $observer->getEvent()->getShipment();
294
$order = $shipment->getOrder();
295
296
- $xml = Mage::getModel('channelunity/orders')->generateCuXmlForOrderShip($order, "", "", "");
297
- $result = $this->postToChannelUnity($xml, "OrderStatusUpdate");
298
- Mage::log('shipAmazon: ' . $result);
299
} catch (Exception $e) {
300
-
301
Mage::logException($e);
302
}
303
}
@@ -308,48 +311,64 @@ class Camiloo_Channelunity_Model_Observer extends Camiloo_Channelunity_Model_Abs
308
public function categorySave(Varien_Event_Observer $observer)
309
{
310
try {
311
- $myStoreURL = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
312
- $categoryStatus = Mage::getModel('channelunity/categories')->postCategoriesToCU($myStoreURL);
313
- } catch (Exception $e) {
314
315
Mage::logException($e);
316
-
317
}
318
}
319
320
- public function configSaveAfter(Varien_Event_Observer $observer)
321
{
322
try {
323
- if (is_object($observer)) {
324
- $event = $observer->getEvent();
325
-
326
- if (is_object($event)) {
327
-
328
- $configData = $event->getData('config_data');
329
330
- if (is_object($configData)) {
331
-
332
- $configData = $configData->getData();
333
-
334
- if (isset($configData['fieldset_data'])) {
335
-
336
- $fieldset_data = $configData['fieldset_data'];
337
338
339
- if (isset($fieldset_data['merchantname'])) {
340
341
- $merchantName = $fieldset_data['merchantname'];
342
343
- Mage::getModel('channelunity/products')->postMyURLToChannelUnity($merchantName);
344
- }
345
- }
346
- }
347
- }
348
- }
349
} catch (Exception $e) {
350
351
Mage::logException($e);
352
-
353
}
354
}
355
@@ -361,35 +380,81 @@ class Camiloo_Channelunity_Model_Observer extends Camiloo_Channelunity_Model_Abs
361
*/
362
public function storeDelete(Varien_Event_Observer $observer)
363
{
364
-
365
- $event = $observer->getEvent();
366
- $store = $event->getStore();
367
-
368
try {
369
370
371
- $xml = "<StoreDelete>\n";
372
- $xml .= "<SourceURL>" . Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB)
373
- . "</SourceURL>\n";
374
-
375
- $storeViewId = $store->getId();
376
- $storeId = $store->getGroupId();
377
- $websiteId = $store->getWebsiteId();
378
-
379
- $xml .= "<StoreId>" . $storeId . "</StoreId>\n";
380
- $xml .= "<DeletedStoreViewId>" . $storeViewId . "</DeletedStoreViewId>\n";
381
- $xml .= "<WebsiteId>" . $websiteId . "</WebsiteId>\n";
382
383
- $xml .="</StoreDelete>\n";
384
385
386
387
- $result = $this->postToChannelUnity($xml, "storeDelete");
388
389
} catch (Exception $e) {
390
-
391
Mage::logException($e);
392
}
393
}
394
395
}
17
{
18
19
/**
20
+ * Allows the observing of more generic events in Magento.
21
+ * Useful in multiple product save for example.
22
*/
23
+ public function hookToControllerActionPostDispatch($observer)
24
+ {
25
try {
26
27
+ // Get event name
28
+ $eventName = $observer->getEvent()->getControllerAction()
29
+ ->getFullActionName();
30
31
+ // Perform tasks
32
+ switch ($eventName) {
33
+ case 'adminhtml_catalog_category_save':
34
35
+ // Save category
36
+ $this->categorySave($observer);
37
38
+ break;
39
+ case 'adminhtml_catalog_category_delete':
40
41
+ // Delete category
42
+ $this->categoryDelete($observer);
43
44
+ break;
45
+ case 'adminhtml_catalog_product_action_attribute_save':
46
47
+ // Set variables
48
+ $helper = Mage::helper(
49
+ 'adminhtml/catalog_product_edit_action_attribute'
50
+ );
51
+ $storeViewId = $helper->getSelectedStoreId();
52
+ $productIds = $helper->getProductIds();
53
+ $data = '';
54
55
+ // Add products
56
+ foreach ($productIds as $id) {
57
+ $data .= Mage::getModel('channelunity/products')
58
+ ->generateCuXmlForSingleProduct(
59
+ $id, $storeViewId
60
+ );
61
+ }
62
63
+ // Send to CU
64
+ $this->_updateProductData($storeViewId, $data);
65
66
+ break;
67
+ case 'adminhtml_catalog_product_delete':
68
+
69
+ // Set variables
70
+ $storeViewId = Mage::helper(
71
+ 'adminhtml/catalog_product_edit_action_attribute'
72
+ )
73
+ ->getSelectedStoreId();
74
+ $data = '<DeletedProductId>' . $observer->getEvent()
75
+ ->getControllerAction()->getRequest()
76
+ ->getParam('id') . '</DeletedProductId>';
77
78
+ // Send to CU
79
+ $this->_updateProductData($storeViewId, $data);
80
81
+ break;
82
+ case 'adminhtml_catalog_product_massStatus':
83
84
+ // Check for product ids
85
+ $productIds = $observer->getEvent()->getControllerAction()
86
+ ->getRequest()->getParam('product');
87
+ if (!is_array($productIds) || !count($productIds)) {
88
+ break;
89
+ }
90
+
91
+ // Set variables
92
+ $storeViewId = Mage::helper(
93
+ 'adminhtml/catalog_product_edit_action_attribute'
94
+ )
95
+ ->getSelectedStoreId();
96
+ $data = '';
97
+
98
+ // Add product data
99
+ foreach ($productIds as $id) {
100
+
101
+ // Load product
102
+ $product = Mage::getModel('catalog/product')
103
+ ->load($id);
104
+ $skipProduct = Mage::getModel('channelunity/products')
105
+ ->skipProduct($product);
106
+
107
+ // Add XML
108
+ if ($skipProduct) {
109
+ $data .= '<DeletedProductId>' . $id
110
+ . '</DeletedProductId>';
111
+ } else {
112
+ $data .= Mage::getModel('channelunity/products')
113
+ ->generateCuXmlForSingleProduct(
114
+ $id, $storeViewId
115
+ );
116
+ }
117
+ }
118
+
119
+ // Send to CU
120
+ $this->_updateProductData($storeViewId, $data);
121
+
122
+ break;
123
+ default:
124
+ break;
125
+ }
126
} catch (Exception $e) {
127
Mage::logException($e);
128
}
129
}
130
131
/**
132
+ * Called on saving a product in Magento.
133
*/
134
+ public function productWasSaved(Varien_Event_Observer $observer)
135
{
136
try {
137
138
+ // Load product
139
+ $product = $observer->getEvent()->getProduct();
140
+ $skipProduct = Mage::getModel('channelunity/products')
141
+ ->skipProduct($product);
142
143
+ // Set variables
144
+ $stores = Mage::app()->getStores();
145
+ $keys = array_keys($stores);
146
147
+ // Make sure the product exists in the first place
148
+ if ($skipProduct) {
149
+ $data = '<DeletedProductId>' . $product->getId()
150
+ . '</DeletedProductId>';
151
+ } else {
152
+ $data = Mage::getModel('channelunity/products')
153
+ ->generateCuXmlForSingleProduct($product->getId(), 0);
154
+ $this->_updateProductData(0, $data);
155
+ }
156
157
+ // Loop through stores
158
+ foreach ($keys as $i) {
159
+
160
+ // Set variables
161
+ $storeViewId = Mage::app()->getStore($i)->getId();
162
+ if ($skipProduct) {
163
+ $data = '<DeletedProductId>' . $product->getId()
164
+ . '</DeletedProductId>';
165
+ } else {
166
+ $data = Mage::getModel('channelunity/products')
167
+ ->generateCuXmlForSingleProduct(
168
+ $product->getId(), $storeViewId
169
+ );
170
}
171
172
+ // Send to CU
173
+ $this->_updateProductData($storeViewId, $data);
174
+ }
175
} catch (Exception $e) {
176
Mage::logException($e);
177
}
178
}
179
180
/**
181
+ * Called on deleting a product in Magento.
182
*/
183
+ public function productWasDeleted(Varien_Event_Observer $observer)
184
{
185
try {
186
187
+ // Load product
188
+ $product = $observer->getEvent()->getProduct();
189
190
+ // Set variables
191
+ $storeViewId = $product->getStoreId();
192
+ $data = '<DeletedProductId>' . $product->getId()
193
+ . '</DeletedProductId>';
194
195
+ // Send to CU
196
+ $this->_updateProductData($storeViewId, $data);
197
} catch (Exception $e) {
198
Mage::logException($e);
199
}
200
}
201
202
+ /**
203
+ * Called on placing an order. Stock levels are updated on CU.
204
+ */
205
+ public function orderWasPlaced(Varien_Event_Observer $observer)
206
{
207
try {
208
209
+ // Update order products
210
+ $this->_updateOrderProducts($observer->getEvent()->getOrder());
211
+ } catch (Exception $e) {
212
Mage::logException($e);
213
}
214
}
215
216
+ /**
217
+ * Called on an invoice being paid. Stock levels are updated on CU.
218
+ */
219
public function onInvoicePaid(Varien_Event_Observer $observer)
220
{
221
try {
222
223
+ // Update order products
224
+ $this->_updateOrderProducts($observer->getInvoice()->getOrder());
225
} catch (Exception $e) {
226
Mage::logException($e);
227
}
233
public function checkForCancellation(Varien_Event_Observer $observer)
234
{
235
try {
236
+
237
+ // Load order
238
$order = $observer->getOrder();
239
240
+ // Create XML
241
+ $xml = Mage::getModel('channelunity/orders')
242
+ ->generateCuXmlForOrderStatus($order);
243
+
244
+ // Send XML to CU
245
+ $this->postToChannelUnity($xml, 'OrderStatusUpdate');
246
} catch (Exception $e) {
247
Mage::logException($e);
248
}
254
public function saveTrackingToAmazon(Varien_Event_Observer $observer)
255
{
256
try {
257
+
258
+ // Set variables
259
$track = $observer->getEvent()->getTrack();
260
$order = $track->getShipment()->getOrder();
261
+ $carrier = $track->getCarrierCode();
262
263
+ // Check carrier
264
+ if ($carrier == 'custom') {
265
+ $carrier = $track->getTitle();
266
}
267
268
+ // Create XML
269
+ $xml = Mage::getModel('channelunity/orders')
270
+ ->generateCuXmlForOrderShip(
271
+ $order, $carrier, $track->getTitle(), $track->getNumber()
272
+ );
273
+ // Send XML to CU
274
+ if (!empty($xml)) {
275
+ $result = $this->postToChannelUnity($xml, 'OrderStatusUpdate');
276
+ Mage::log('saveTrackingToAmazon: ' . $result);
277
+ } else {
278
+ Mage::log('Nothing to ship');
279
+ }
280
} catch (Exception $e) {
281
Mage::logException($e);
282
}
285
public function shipAmazon(Varien_Event_Observer $observer)
286
{
287
try {
288
+
289
+ // Set variables
290
$shipment = $observer->getEvent()->getShipment();
291
$order = $shipment->getOrder();
292
293
+ // Create XML
294
+ $xml = Mage::getModel('channelunity/orders')
295
+ ->generateCuXmlForOrderShip($order, '', '', '');
296
+ // Send XML to CU
297
+ if (!empty($xml)) {
298
+ $result = $this->postToChannelUnity($xml, 'OrderStatusUpdate');
299
+ Mage::log('shipAmazon: ' . $result);
300
+ } else {
301
+ Mage::log('Nothing to ship');
302
+ }
303
} catch (Exception $e) {
304
Mage::logException($e);
305
}
306
}
311
public function categorySave(Varien_Event_Observer $observer)
312
{
313
try {
314
315
+ // Send categories
316
+ Mage::getModel('channelunity/categories')->postCategoriesToCU(
317
+ Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB)
318
+ );
319
+ } catch (Exception $e) {
320
Mage::logException($e);
321
}
322
}
323
324
+ /**
325
+ * Triggers on a category delete event. Removes category data in CU.
326
+ * @author Gary Lockett
327
+ *
328
+ * @param Varien_Event_Observer $observer
329
+ */
330
+ public function categoryDelete(Varien_Event_Observer $observer)
331
{
332
try {
333
334
+ // Set variables
335
+ $sourceUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
336
+ $categoryId = $observer->getEvent()->getControllerAction()
337
+ ->getRequest()->getParam('id');
338
+
339
+ // Create XML
340
+ $xml = <<<XML
341
+ <CategoryDelete>
342
+ <SourceURL>{$sourceUrl}</SourceURL>
343
+ <DeletedCategoryId>{$categoryId}</DeletedCategoryId>
344
+ </CategoryDelete>
345
+ XML;
346
+ // Send XML to CU
347
+ $this->postToChannelUnity($xml, 'categoryDelete');
348
+ } catch (Exception $e) {
349
+ Mage::logException($e);
350
+ }
351
+ }
352
353
+ /**
354
+ * Configuration data is updated. CU needs to know about it.
355
+ */
356
+ public function configSaveAfter(Varien_Event_Observer $observer)
357
+ {
358
+ try {
359
360
+ // Load configuration
361
+ $config = $observer->getEvent()->getData('config_data')->getData();
362
363
+ // Set variables
364
+ $merchant = $config['fieldset_data']['merchantname'];
365
366
+ // Send to CU
367
+ Mage::getModel('channelunity/products')
368
+ ->postMyURLToChannelUnity($merchant);
369
} catch (Exception $e) {
370
371
Mage::logException($e);
372
}
373
}
374
380
*/
381
public function storeDelete(Varien_Event_Observer $observer)
382
{
383
try {
384
385
+ // Load store
386
+ $store = $observer->getEvent()->getStore();
387
+
388
+ // Set variables
389
+ $sourceUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
390
+
391
+ // Create XML
392
+ $xml = <<<XML
393
+ <StoreDelete>
394
+ <SourceURL>{$sourceUrl}</SourceURL>
395
+ <StoreId>{$store->getGroupId()}</StoreId>
396
+ <DeletedStoreViewId>{$store->getId()}</DeletedStoreViewId>
397
+ <WebsiteId>{$store->getWebsiteId()}</WebsiteId>
398
+ </StoreDelete>
399
+ XML;
400
+ // Send XML to CU
401
+ $this->postToChannelUnity($xml, 'storeDelete');
402
+ } catch (Exception $e) {
403
+ Mage::logException($e);
404
+ }
405
+ }
406
407
+ /**
408
+ * PRIVATE METHODS
409
+ */
410
+ private function _updateOrderProducts($order)
411
+ {
412
+ try {
413
414
+ // Set variables
415
+ $items = $order->getAllItems();
416
+ $storeViewId = $order->getStore()->getId();
417
+ $data = '';
418
419
+ // Loop through items
420
+ foreach ($items as $item) {
421
422
+ // Load product
423
+ $product = Mage::getModel('catalog/product')
424
+ ->loadByAttribute('sku', $item->getSku());
425
+ if (!$product) {
426
+ continue;
427
+ }
428
429
+ // Add XML
430
+ $data .= Mage::getModel('channelunity/products')
431
+ ->generateCuXmlForSingleProduct(
432
+ $product->getId(), $storeViewId, 0
433
+ );
434
+ }
435
436
+ // Send to CU
437
+ $this->_updateProductData($storeViewId, $data);
438
} catch (Exception $e) {
439
Mage::logException($e);
440
}
441
}
442
443
+ private function _updateProductData($storeViewId, $data)
444
+ {
445
+ // Set variables
446
+ $sourceUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
447
+
448
+ // Create XML
449
+ $xml = <<<XML
450
+ <Products>
451
+ <SourceURL>{$sourceUrl}</SourceURL>
452
+ <StoreViewId>{$storeViewId}</StoreViewId>
453
+ {$data}
454
+ </Products>
455
+ XML;
456
+ // Send XML to CU
457
+ return $this->postToChannelUnity($xml, 'ProductData');
458
+ }
459
+
460
}
app/code/community/Camiloo/Channelunity/Model/Ordercreatebackport.php CHANGED
@@ -1,4 +1,5 @@
1
<?php
2
/**
3
* This code is backported from the Magento codebase at v1.4.0.0 to ensure
4
* compatibility across Magento versions older than v1.4. Its v1.4 classname is
@@ -15,6 +16,7 @@
15
*/
16
class Camiloo_Channelunity_Model_Ordercreatebackport
17
{
18
/**
19
* Quote object
20
*
@@ -43,8 +45,8 @@ class Camiloo_Channelunity_Model_Ordercreatebackport
43
*/
44
public function __construct(Mage_Sales_Model_Quote $quote)
45
{
46
- $this->_quote = $quote;
47
- $this->_convertor = Mage::getModel('sales/convert_quote');
48
}
49
50
/**
@@ -130,10 +132,10 @@ class Camiloo_Channelunity_Model_Ordercreatebackport
130
/**
131
* We can use configuration data for declare new order status
132
*/
133
- Mage::dispatchEvent('checkout_type_onepage_save_order', array('order'=>$order, 'quote'=>$quote));
134
- Mage::dispatchEvent('sales_model_service_quote_submit_before', array('order'=>$order, 'quote'=>$quote));
135
$transaction->save();
136
- Mage::dispatchEvent('sales_model_service_quote_submit_after', array('order'=>$order, 'quote'=>$quote));
137
return $order;
138
}
139
@@ -150,11 +152,11 @@ class Camiloo_Channelunity_Model_Ordercreatebackport
150
$addressValidation = $address->validate();
151
if ($addressValidation !== true) {
152
Mage::throwException(
153
- $helper->__('Please check shipping address information. %s', implode(' ', $addressValidation))
154
);
155
}
156
- $method= $address->getShippingMethod();
157
- $rate = $address->getShippingRateByCode($method);
158
if (!$this->getQuote()->isVirtual() && (!$method || !$rate)) {
159
Mage::throwException($helper->__('Please specify shipping method.'));
160
}
@@ -163,7 +165,7 @@ class Camiloo_Channelunity_Model_Ordercreatebackport
163
$addressValidation = $this->getQuote()->getBillingAddress()->validate();
164
if ($addressValidation !== true) {
165
Mage::throwException(
166
- $helper->__('Please check billing address information. %s', implode(' ', $addressValidation))
167
);
168
}
169
@@ -172,4 +174,5 @@ class Camiloo_Channelunity_Model_Ordercreatebackport
172
}
173
return $this;
174
}
175
}
1
<?php
2
+
3
/**
4
* This code is backported from the Magento codebase at v1.4.0.0 to ensure
5
* compatibility across Magento versions older than v1.4. Its v1.4 classname is
16
*/
17
class Camiloo_Channelunity_Model_Ordercreatebackport
18
{
19
+
20
/**
21
* Quote object
22
*
45
*/
46
public function __construct(Mage_Sales_Model_Quote $quote)
47
{
48
+ $this->_quote = $quote;
49
+ $this->_convertor = Mage::getModel('sales/convert_quote');
50
}
51
52
/**
132
/**
133
* We can use configuration data for declare new order status
134
*/
135
+ Mage::dispatchEvent('checkout_type_onepage_save_order', array('order' => $order, 'quote' => $quote));
136
+ Mage::dispatchEvent('sales_model_service_quote_submit_before', array('order' => $order, 'quote' => $quote));
137
$transaction->save();
138
+ Mage::dispatchEvent('sales_model_service_quote_submit_after', array('order' => $order, 'quote' => $quote));
139
return $order;
140
}
141
152
$addressValidation = $address->validate();
153
if ($addressValidation !== true) {
154
Mage::throwException(
155
+ $helper->__('Please check shipping address information. %s', implode(' ', $addressValidation))
156
);
157
}
158
+ $method = $address->getShippingMethod();
159
+ $rate = $address->getShippingRateByCode($method);
160
if (!$this->getQuote()->isVirtual() && (!$method || !$rate)) {
161
Mage::throwException($helper->__('Please specify shipping method.'));
162
}
165
$addressValidation = $this->getQuote()->getBillingAddress()->validate();
166
if ($addressValidation !== true) {
167
Mage::throwException(
168
+ $helper->__('Please check billing address information. %s', implode(' ', $addressValidation))
169
);
170
}
171
174
}
175
return $this;
176
}
177
+
178
}
app/code/community/Camiloo/Channelunity/Model/Orders.php CHANGED
@@ -1,6 +1,7 @@
1
<?php
2
/**
3
- * ChannelUnity connector for Magento Commerce
4
*
5
* @category Camiloo
6
* @package Camiloo_Channelunity
@@ -8,460 +9,532 @@
8
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
9
*/
10
class Camiloo_Channelunity_Model_Orders extends Camiloo_Channelunity_Model_Abstract
11
- {
12
- protected $_collection = 'sales/order';
13
14
/*
15
16
- RequestType OrderStatusUpdate
17
-
18
- Payload XML Message
19
- <SubscriptionID /> The corresponding ChannelUnity subscription ID
20
- <OrderID /> The channel specific order ID being shipped
21
- <OrderStatus /> The new order status
22
-
23
- If being shipped / completed:
24
- <ShipmentDate /> The date and time the item was shipped
25
- <CarrierName /> The name of the delivery company
26
- <ShipmentMethod /> The shipping method used
27
- <TrackingNumber /> The tracking number for the shipment
28
*/
29
- public function generateCuXmlForOrderStatus($order) {
30
$orderStatus = $order->getState();
31
-
32
if ($orderStatus == 'canceled') {
33
$orderStatus = "Cancelled";
34
- }
35
- else if ($orderStatus == 'closed') {
36
$orderStatus = "Cancelled";
37
- }
38
- else if ($orderStatus == 'complete') {
39
$orderStatus = "Complete";
40
- }
41
- else if ($orderStatus == 'processing') {
42
$orderStatus = "Processing";
43
- }
44
- else if ($orderStatus == 'holded') {
45
$orderStatus = "OnHold";
46
- }
47
- else if ($orderStatus == 'new') {
48
$orderStatus = "Processing";
49
- }
50
- else if ($orderStatus == 'payment_review') {
51
$orderStatus = "OnHold";
52
- }
53
- else if ($orderStatus == 'pending_payment') {
54
$orderStatus = "OnHold";
55
- }
56
- else if ($orderStatus == 'fraud') {
57
$orderStatus = "OnHold";
58
- }
59
- else {
60
$orderStatus = "Processing";
61
}
62
-
63
$collection = Mage::getModel('sales/order_payment_transaction')->getCollection()
64
- ->setOrderFilter($order);
65
-
66
$orderXml = "";
67
-
68
foreach ($collection as $txn) {
69
$infoArray = $txn->getAdditionalInformation();
70
if (isset($infoArray['SubscriptionId'])) {
71
$orderXml .= "<SubscriptionID>{$infoArray['SubscriptionId']}</SubscriptionID>\n";
72
}
73
if (isset($infoArray['RemoteOrderID'])) {
74
$orderXml .= "<OrderID>{$infoArray['RemoteOrderID']}</OrderID>\n";
75
}
76
break;
77
}
78
$orderXml .= "<OrderStatus>$orderStatus</OrderStatus>\n";
79
-
80
return $orderXml;
81
}
82
-
83
- public function generateCuXmlForOrderShip($order,
84
- $carrierName,
85
- $shipMethod,
86
- $trackNumber) {
87
-
88
- $orderXml = $this->generateCuXmlForOrderStatus($order);
89
-
90
- $orderXml .= "<ShipmentDate>".date("c")."</ShipmentDate>\n";
91
- $orderXml .= "<CarrierName>$carrierName</CarrierName>\n";
92
- $orderXml .= "<ShipmentMethod>$shipMethod</ShipmentMethod>\n";
93
- $orderXml .= "<TrackingNumber>$trackNumber</TrackingNumber>\n";
94
-
95
return $orderXml;
96
}
97
-
98
- public function fixEncoding($in_str)
99
- {
100
- if (function_exists('mb_strlen')) {
101
-
102
- $cur_encoding = mb_detect_encoding($in_str);
103
- if ($cur_encoding == "UTF-8" && mb_check_encoding($in_str, "UTF-8")) {
104
-
105
- } else {
106
- $in_str = utf8_encode($in_str);
107
- }
108
- }
109
-
110
- return $in_str;
111
- }
112
-
113
- public function doCreate($dataArray, $order) {
114
- // this method takes an array of the normal structure and creates an
115
- // order creation request within Magento.
116
-
117
- echo "<Info>Next order: {$order->OrderId} Create Quote</Info>";
118
- Mage::register('cu_order_in_progress',1);
119
- try {
120
-
121
- $quote = Mage::getModel('sales/quote')->setStoreId((string) $dataArray->StoreviewId);
122
-
123
- // we need to verify (from our XML) that we can create customer accounts
124
- // and that we can contact the customer.
125
-
126
-
127
echo "<Info>Create Customer</Info>";
128
129
-
130
- $customer = Mage::getModel('customer/customer')
131
- ->setWebsiteId((string) $dataArray->WebsiteId)
132
- ->loadByEmail((string) $order->BillingInfo->Email);
133
-
134
- if ($customer->getId() > 0) {
135
- $quote->assignCustomer($customer);
136
- } else {
137
- if ((string) $order->customer->canCreateCustomer) {
138
- // customer does not exist, but we can create one.
139
- // however. if we can't email the customer their password
140
- // there's no point continuing.
141
- if ((string) $order->customer->canEmailCustomer) {
142
-
143
- // we can create a customer, and can email them a random password
144
- // within a welcome email. So lets do this.
145
-
146
- $customer = Mage::getModel('customer/customer');
147
-
148
- $customerData = array(
149
- "firstname" => $this->fixEncoding((string) $order->customer->firstname),
150
- "lastname" => $this->fixEncoding((string) $order->customer->surname),
151
- "email" => (string) $order->customer->email,
152
- "website_id" => (string) $order->websiteId,
153
- );
154
-
155
- $customer->addData($customerData);
156
- $customer->save();
157
- $customer->setPassword($customer->generatePassword(8))->save();
158
- $customer->sendNewAccountEmail();
159
- $customer->save();
160
-
161
- // and now to assign the customer onto the quote.
162
- $quote->assignCustomer($customer);
163
-
164
- } else {
165
- // create the order as a guest.
166
- $quote->setCustomerFirstname($this->fixEncoding((string) $order->customer->firstname));
167
- $quote->setCustomerLastname($this->fixEncoding((string) $order->customer->surname));
168
- $quote->setCustomerEmail((string) $order->customer->email);
169
- $quote->setCustomerIsGuest(1);
170
- }
171
-
172
} else {
173
- // create the order as a guest.
174
- $quote->setCustomerFirstname($this->fixEncoding($this->getFirstName((string) $order->BillingInfo->Name)));
175
- $quote->setCustomerLastname($this->fixEncoding($this->getLastName((string) $order->BillingInfo->Name)));
176
- $quote->setCustomerEmail((string) $order->BillingInfo->Email);
177
- $quote->setCustomerIsGuest(1);
178
}
179
}
180
-
181
echo "<Info>Order currency {$order->Currency}</Info>";
182
-
183
$quote->getStore()->setCurrentCurrencyCode((string) $order->Currency);
184
-
185
$storeCurrency = $quote->getStore()->getBaseCurrencyCode();
186
-
187
echo "<Info>Store currency $storeCurrency</Info>";
188
-
189
$currencyObject = Mage::getModel('directory/currency');
190
$reverseRate = $currencyObject->getResource()->getRate($storeCurrency, (string) $order->Currency);
191
-
192
if ($reverseRate == "") {
193
$reverseRate = 1.0;
194
}
195
196
echo "<ConversionRate>$reverseRate</ConversionRate>";
197
$itemOptions = array();
198
199
- // add product(s)
200
- foreach ($order->OrderItems->Item as $orderitem) {
201
- $product = Mage::getModel('catalog/product')->loadByAttribute(
202
- (string) $dataArray->SkuAttribute,
203
- (string) $orderitem->SKU);
204
-
205
// First check if this is a custom option
206
if (!is_object($product)) {
207
$skuparts = explode("-", (string) $orderitem->SKU);
208
-
209
if (count($skuparts) > 1) {
210
$parentsku = $skuparts[0];
211
-
212
$product = Mage::getModel('catalog/product')->loadByAttribute(
213
- (string) $dataArray->SkuAttribute,
214
- $parentsku);
215
-
216
for ($i = 1; $i < count($skuparts); $i++) {
217
$itemOptions[$parentsku][] = $skuparts[$i];
218
}
219
}
220
}
221
// ------------------------------------------------------
222
-
223
if (is_object($product)) {
224
-
225
- $product->setPrice( $this->getDeTaxPrice((string) $orderitem->Price) / $reverseRate);
226
-
227
$item = Mage::getModel('sales/quote_item');
228
$item->setQuote($quote)->setProduct($product);
229
$item->setData('qty', (string) $orderitem->Quantity);
230
- $item->setCustomPrice($this->getDeTaxPrice((string) $orderitem->Price));
231
- $item->setOriginalCustomPrice($this->getDeTaxPrice((string) $orderitem->Price));
232
-
233
$quote->addItem($item);
234
-
235
$quote->save();
236
$item->save();
237
- }
238
- else {
239
- echo "<Info>Can't find SKU to add to quote ".((string) $orderitem->SKU)
240
- .", trying to create stub</Info>";
241
-
242
$prodIdToLoad = 0;
243
-
244
try {
245
// Create stub if needed
246
- $this->createStubProduct((string) $orderitem->SKU, (string) $orderitem->Name,
247
- (string) $dataArray->WebsiteId,
248
- (string) $order->OrderId, (string) $orderitem->Price,
249
- (string) $orderitem->Quantity,
250
- (string) $dataArray->SkuAttribute);
251
- }
252
- catch (Exception $e) {
253
- echo "<Info><![CDATA[Stub create error - ".$e->getMessage()."]]></Info>";
254
-
255
if (strpos($e->getMessage(), "Duplicate entry")) {
256
-
257
$msgParts = explode("Duplicate entry '", $e->getMessage());
258
-
259
if (isset($msgParts[1])) {
260
-
261
$msgParts = $msgParts[1];
262
$msgParts = explode("-", $msgParts);
263
$prodIdToLoad = $msgParts[0];
264
}
265
}
266
}
267
-
268
if ($prodIdToLoad > 0) {
269
-
270
echo "<Info>Load by ID $prodIdToLoad</Info>";
271
$product = Mage::getModel('catalog/product')->load($prodIdToLoad);
272
- }
273
- else {
274
-
275
// Try once again to add our item to the quote
276
- $product = Mage::getModel('catalog/product')->loadByAttribute('sku',
277
- (string) $orderitem->SKU);
278
-
279
}
280
-
281
if (is_object($product)) {
282
-
283
- $product->setPrice($this->getDeTaxPrice((string) $orderitem->Price) / $reverseRate);
284
-
285
$item = Mage::getModel('sales/quote_item');
286
$item->setQuote($quote)->setProduct($product);
287
$item->setData('qty', (string) $orderitem->Quantity);
288
- $item->setCustomPrice($this->getDeTaxPrice((string) $orderitem->Price));
289
- $item->setOriginalCustomPrice($this->getDeTaxPrice((string) $orderitem->Price));
290
$quote->addItem($item);
291
-
292
$quote->save();
293
$item->save();
294
- }
295
- else {
296
- echo "<Info>Can't find SKU to add to quote ".((string) $orderitem->SKU)."</Info>";
297
}
298
}
299
- }
300
-
301
- echo "<Info>Set Billing Address</Info>";
302
-
303
- $postcode = $this->fixEncoding((string) $order->ShippingInfo->PostalCode);
304
-
305
- $regionModel = Mage::getModel('directory/region')->loadByCode((string) $order->ShippingInfo->State, (string) $order->ShippingInfo->Country);
306
- $regionId = is_object($regionModel) ? $regionModel->getId() : ((string) $order->ShippingInfo->State);
307
-
308
- // set the billing address
309
- $billingAddressData = array(
310
- 'firstname' => $this->fixEncoding($this->getFirstName((string) $order->BillingInfo->Name)),
311
- 'lastname' => $this->fixEncoding($this->getLastName((string) $order->BillingInfo->Name)),
312
- 'email' => (string) $order->BillingInfo->Email,
313
- 'telephone' => ( (string) $order->BillingInfo->PhoneNumber == "" ?
314
- (string) $order->ShippingInfo->PhoneNumber :
315
- (string) $order->BillingInfo->PhoneNumber),
316
-
317
- 'street' => (string) $this->fixEncoding((string) $order->ShippingInfo->Address1."\n"
318
- .(string) $order->ShippingInfo->Address2
319
- ."\n".(string) $order->ShippingInfo->Address3),
320
- 'city' => $this->fixEncoding((string) $order->ShippingInfo->City),
321
- 'postcode' => $postcode,
322
- 'region' => (string) $order->ShippingInfo->State,
323
- 'region_id' => $regionId,
324
- 'country_id' => (string) $order->ShippingInfo->Country,
325
- 'should_ignore_validation' => true
326
- );
327
-
328
- // add the billing address to the quote.
329
- $billingAddress = $quote->getBillingAddress()->addData($billingAddressData);
330
-
331
- echo "<Info>Set Shipping Address</Info>";
332
-
333
- // set the shipping address
334
- $shippingAddressData = array(
335
- 'firstname' => $this->fixEncoding($this->getFirstName((string) $order->ShippingInfo->RecipientName)),
336
- 'lastname' => $this->fixEncoding($this->getLastName((string) $order->ShippingInfo->RecipientName)),
337
- 'street' => (string) $this->fixEncoding((string) $order->ShippingInfo->Address1
338
- ."\n".(string) $order->ShippingInfo->Address2
339
- ."\n".(string) $order->ShippingInfo->Address3),
340
- 'city' => $this->fixEncoding((string) $order->ShippingInfo->City),
341
- 'postcode' => $postcode,
342
- 'region' => (string) $order->ShippingInfo->State,
343
- 'region_id' => $regionId,
344
- 'country_id' => (string) $order->ShippingInfo->Country,
345
- 'telephone' => (string) $order->ShippingInfo->PhoneNumber,
346
- 'should_ignore_validation' => true
347
- );
348
-
349
- Mage::getSingleton('core/session')->setShippingPrice(
350
- ((string) $order->ShippingInfo->ShippingPrice) / $reverseRate);
351
-
352
- // add the shipping address to the quote.
353
- $shippingAddress = $quote->getShippingAddress()->addData($shippingAddressData);
354
- $quote->getShippingAddress()->setData('should_ignore_validation', true);
355
- $quote->getBillingAddress()->setData('should_ignore_validation', true);
356
/////////////////////////////////////////////
357
- $method = Mage::getModel('shipping/rate_result_method');
358
- $method->setCarrier('channelunitycustomrate');
359
- $method->setCarrierTitle('ChannelUnity Shipping');
360
- $method->setMethod('channelunitycustomrate');
361
- $method->setMethodTitle((string) $order->ShippingInfo->Service);
362
-
363
- $shipPrice = Mage::getSingleton('core/session')->getShippingPrice();
364
-
365
- $method->setPrice($shipPrice);
366
- $method->setCost($shipPrice);
367
-
368
- $rate = Mage::getModel('sales/quote_address_rate')
369
- ->importShippingRate($method);
370
-
371
- $shippingAddress->addShippingRate($rate);
372
-
373
/////////////////////////////////////////////
374
- $shippingAddress->setShippingMethod('channelunitycustomrate_channelunitycustomrate');
375
- $shippingAddress->setShippingDescription((string) $order->ShippingInfo->Service);
376
- $shippingAddress->setPaymentMethod('channelunitypayment');
377
-
378
$quote->getPayment()->importData(array(
379
- 'method' => 'channelunitypayment'
380
- ));
381
$quote->collectTotals()->save();
382
-
383
- if (version_compare(Mage::getVersion(), "1.4.0.0", ">=")) {
384
- $service = Mage::getModel('sales/service_quote', $quote);
385
- } else {
386
- $service = Mage::getModel('channelunity/ordercreatebackport', $quote);
387
- }
388
-
389
- $currentstore = Mage::app()->getStore()->getId();
390
- // upgrade to admin permissions to avoid item qty not available issue
391
- Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
392
-
393
- $service->submitAll();
394
- $newOrder = $service->getOrder(); // returns full order object.
395
- // we're done; sign out of admin permission
396
- Mage::app()->setCurrentStore($currentstore);
397
-
398
if (!is_object($newOrder)) {
399
- echo "<NotImported>".((string) $order->OrderId)."</NotImported>";
400
return;
401
}
402
- else {
403
- echo "<Imported>".((string) $order->OrderId)."</Imported>";
404
}
405
-
406
- } catch (Exception $x) {
407
- echo "<Exception><![CDATA[".$x->getMessage()." ".$x->getTraceAsString()."]]></Exception>";
408
- echo "<NotImported>".((string) $order->OrderId)."</NotImported>";
409
- Mage::unregister('cu_order_in_progress');
410
- return;
411
- }
412
-
413
- $ordStatus = $this->CUOrderStatusToMagentoStatus((string) $order->OrderStatus);
414
-
415
try {
416
- $newOrder->setData('state', $ordStatus);
417
- $newOrder->setStatus($ordStatus);
418
- $history = $newOrder->addStatusHistoryComment(
419
- 'Order imported from ChannelUnity', false);
420
- $history->setIsCustomerNotified(false);
421
422
}
423
- catch (Exception $x1) {
424
-
425
- try {
426
- $newOrder->setState('closed', 'closed',
427
- 'Order imported from ChannelUnity', false);
428
- }
429
- catch (Exception $x2) {
430
- }
431
- }
432
-
433
- // This order will have been paid for, otherwise it won't
434
// have imported
435
-
436
$invoiceId = Mage::getModel('sales/order_invoice_api')
437
- ->create($newOrder->getIncrementId(), array());
438
-
439
$invoice = Mage::getModel('sales/order_invoice')
440
-