Skip to content

Commit 05d0a10

Browse files
committedFeb 7, 2019
Make syncProduct support array of product ids
1 parent 8f4bd5b commit 05d0a10

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed
 

‎code/Model/Communicator.php

+28-22
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,46 @@ class Clerk_Clerk_Model_Communicator extends Mage_Core_Helper_Abstract
1010
const XML_PATH_PUBLIC_KEY = 'clerk/general/publicapikey';
1111
const XML_PATH_PRIVATE_KEY = 'clerk/general/privateapikey';
1212

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
1915
*
20-
* In the future we should find a better solution to this problem.
16+
* @param $productIds
17+
* @throws Mage_Core_Exception
2118
*/
22-
public function syncProduct($productId)
19+
public function syncProduct($productIds)
2320
{
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+
}
2624

27-
foreach ($product->getStoreIds() as $storeId) {
28-
$store_enabled = Mage::helper('clerk')->getSetting('clerk/general/active', $storeId);
25+
$appEmulation = Mage::getSingleton('core/app_emulation');
2926

30-
if (! $store_enabled) {
27+
foreach (Mage::app()->getStores() as $store) {
28+
if (!Mage::helper('clerk')->getSetting('clerk/general/active', $store->getId())) {
3129
continue;
3230
}
3331

34-
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
35-
$product = Mage::getModel('clerk/product')->load($productId);
32+
$productData = [];
3633

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+
}
4444
}
4545

4646
$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);
4753
}
4854
}
4955

‎code/Model/Observer.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ public function syncProducts(Varien_Event_Observer $observer)
6464
{
6565
$productIds = $observer->getEvent()->getProductIds();
6666

67-
foreach ($productIds as $productId) {
68-
Mage::getModel('clerk/communicator')->syncProduct($productId, $observer->getEvent()->getName());
67+
if (!is_array($productIds)) {
68+
$productIds = [$productIds];
6969
}
70+
71+
Mage::getModel('clerk/communicator')->syncProduct($productIds, $observer->getEvent()->getName());
7072
}
7173

7274
/**

‎modman

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Clerk.xml app/etc/modules/Clerk_Clerk.xml
22
code app/code/community/Clerk/Clerk
33
skin skin/frontend/base/default/clerk
4-
design/frontend/template app/design/frontend/base/default/template/clerk
4+
design/frontend/template/ app/design/frontend/base/default/template/clerk/
55
design/frontend/layout/clerk.xml app/design/frontend/base/default/layout/clerk.xml
6-
design/adminhtml/template app/design/adminhtml/default/default/template/clerk
6+
design/adminhtml/template/ app/design/adminhtml/default/default/template/clerk/
77
design/adminhtml/layout/clerk.xml app/design/adminhtml/default/default/layout/clerk.xml
88
tests tests
99
locale/en_US/Clerk_Clerk.csv app/locale/en_US/Clerk_Clerk.csv

0 commit comments

Comments
 (0)
Please sign in to comment.