Skip to content

Commit

Permalink
Merged PR 16744: Mark ordered products as modified
Browse files Browse the repository at this point in the history
Some clients noticed that they get wrong stock values in catalog sync.

This is happening because after the completion of a product purchase we don't
mark it again as `un-processed`. This means that every time a user buys a product
then we don't resync the new decreased stock value.

**What changed**
Now during order sync we mark ordered products as un-processed. By doing this we resync these products during catalog sync, so the new stock value will be updated.

**How to test**
- Simply complete a purchase of a product that is already imported into EC.
- After you run the order sync, then the product should be marked as processed = 0 in `email_catalog`

Related work items: #99017
  • Loading branch information
sertlab committed Oct 22, 2019
1 parent bb2ee3b commit 25878c8
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions code/Dotdigitalgroup/Email/Model/Sales/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ public function sync()
$error = true;
}
}

//if no error then set imported
if (!$error) {
$this->_setImported($orderIdsForSingleSync, true);
}
}


// Mark ordered products as unprocessed
$this->updateCatalog(array_merge($orders, $ordersForSingleSync));
unset($this->accounts[$account->getApiUsername()]);
}

Expand Down Expand Up @@ -178,6 +179,10 @@ protected function _searchAccounts()
Dotdigitalgroup_Email_Helper_Config::XML_PATH_CONNECTOR_TRANSACTIONAL_DATA_SYNC_LIMIT,
$website
);
$orders = $this->getConnectorOrders($website, $limit);
$modifiedOrders = $this->getConnectorOrders($website, $limit, true);

//Set purchased products as unprocessed
if (!isset($this->accounts[$this->apiUsername])) {
$account = Mage::getModel(
'ddg_automation/connector_account'
Expand All @@ -187,9 +192,7 @@ protected function _searchAccounts()
$this->accounts[$this->apiUsername] = $account;
}

$this->accounts[$this->apiUsername]->setOrders(
$this->getConnectorOrders($website, $limit)
);
$this->accounts[$this->apiUsername]->setOrders($orders);
$orderIds = array_merge(
$this->accounts[$this->apiUsername]->getOrderIds(),
$this->orderIds
Expand All @@ -198,9 +201,7 @@ protected function _searchAccounts()
$this->accounts[$this->apiUsername]->setWebsites(
$website->getId()
);
$this->accounts[$this->apiUsername]->setOrdersForSingleSync(
$this->getConnectorOrders($website, $limit, true)
);
$this->accounts[$this->apiUsername]->setOrdersForSingleSync($modifiedOrders);
$orderIdsForSingleSync = array_merge(
$this->accounts[$this->apiUsername]->getOrderIdsForSingleSync(),
$this->orderIdsForSingleSync
Expand Down Expand Up @@ -530,4 +531,23 @@ protected function _setImported($ids, $modified = false)
Mage::logException($e);
}
}

/**
* Marks as Unprocessed the purchased products
* @param array $orders
*/
private function updateCatalog($orders)
{
$skus = [];
foreach ($orders as $order) {
foreach ($order->products as $product) {
$skus[] = $product['sku'];
}
}

$productCollection = Mage::getResourceModel('catalog/product_collection')
->addFieldToFilter('sku', array('in' => array_unique($skus)));

Mage::getResourceModel('ddg_automation/catalog')->setUnProcessed($productCollection->getAllIds());
}
}

0 comments on commit 25878c8

Please sign in to comment.