@@ -10,40 +10,46 @@ class Clerk_Clerk_Model_Communicator extends Mage_Core_Helper_Abstract
10
10
const XML_PATH_PUBLIC_KEY = 'clerk/general/publicapikey ' ;
11
11
const XML_PATH_PRIVATE_KEY = 'clerk/general/privateapikey ' ;
12
12
13
- /*
14
- * This call will connect to the clerk api call either either add a
15
- * product or delete it. Because the module at this time does not store data
16
- * in the magento database we have no way of knowing if the product after
17
- * a given event have changed state (include/exclude), thus we have to sync
18
- * products even though they are already synced.
13
+ /**
14
+ * Sync product(s) with Clerk, removing it if it is excluded
19
15
*
20
- * In the future we should find a better solution to this problem.
16
+ * @param $productIds
17
+ * @throws Mage_Core_Exception
21
18
*/
22
- public function syncProduct ($ productId )
19
+ public function syncProduct ($ productIds )
23
20
{
24
- $ product = Mage::getModel ('clerk/product ' )->load ($ productId );
25
- $ appEmulation = Mage::getSingleton ('core/app_emulation ' );
21
+ if (!is_array ($ productIds )) {
22
+ $ productIds = array ($ productIds );
23
+ }
26
24
27
- foreach ($ product ->getStoreIds () as $ storeId ) {
28
- $ store_enabled = Mage::helper ('clerk ' )->getSetting ('clerk/general/active ' , $ storeId );
25
+ $ appEmulation = Mage::getSingleton ('core/app_emulation ' );
29
26
30
- if (! $ store_enabled ) {
27
+ foreach (Mage::app ()->getStores () as $ store ) {
28
+ if (!Mage::helper ('clerk ' )->getSetting ('clerk/general/active ' , $ store ->getId ())) {
31
29
continue ;
32
30
}
33
31
34
- $ initialEnvironmentInfo = $ appEmulation ->startEnvironmentEmulation ($ storeId );
35
- $ product = Mage::getModel ('clerk/product ' )->load ($ productId );
32
+ $ productData = [];
36
33
37
- if ($ product ->isExcluded ()) {
38
- $ this ->removeProduct ($ productId );
39
- } else {
40
- $ data = $ product ->getClerkExportData ();
41
- $ data ['key ' ] = $ this ->getPublicKey ($ storeId );
42
- $ data ['private_key ' ] = $ this ->getPrivateKey ($ storeId );
43
- $ this ->post ('product/add ' , $ data );
34
+ $ initialEnvironmentInfo = $ appEmulation ->startEnvironmentEmulation ($ store ->getId ());
35
+
36
+ foreach ($ productIds as $ productId ) {
37
+ $ product = Mage::getModel ('clerk/product ' )->load ($ productId );
38
+
39
+ if ($ product ->isExcluded ()) {
40
+ $ this ->removeProduct ($ productId );
41
+ } else {
42
+ $ productData [] = $ product ->getClerkExportData ();
43
+ }
44
44
}
45
45
46
46
$ appEmulation ->stopEnvironmentEmulation ($ initialEnvironmentInfo );
47
+
48
+ $ data ['key ' ] = $ this ->getPublicKey ($ store ->getId ());
49
+ $ data ['private_key ' ] = $ this ->getPrivateKey ($ store ->getId ());
50
+ $ data ['products ' ] = $ productData ;
51
+
52
+ $ this ->post ('product/add ' , $ data );
47
53
}
48
54
}
49
55
0 commit comments