From 25878c80ca8c709a073d1ed2e055d044f67a8e90 Mon Sep 17 00:00:00 2001 From: Fanis Strezos Date: Tue, 22 Oct 2019 10:56:27 +0000 Subject: [PATCH] Merged PR 16744: Mark ordered products as modified 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 --- .../Email/Model/Sales/Order.php | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/code/Dotdigitalgroup/Email/Model/Sales/Order.php b/code/Dotdigitalgroup/Email/Model/Sales/Order.php index 27fac3ed6..0e5bed65f 100755 --- a/code/Dotdigitalgroup/Email/Model/Sales/Order.php +++ b/code/Dotdigitalgroup/Email/Model/Sales/Order.php @@ -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()]); } @@ -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' @@ -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 @@ -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 @@ -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()); + } } \ No newline at end of file