From c84b4bd1887130086b8f01db8514e57e301da70d Mon Sep 17 00:00:00 2001 From: gonzalo Date: Wed, 25 Sep 2024 17:18:50 -0300 Subject: [PATCH 1/3] closes #2003 for magento 2.3.6-p1 --- Helper/Sync.php | 4 + Model/Api/Product.php | 93 ++++++++++++------- Model/MailChimpSyncEcommerce.php | 5 + .../ResourceModel/MailChimpSyncEcommerce.php | 13 +++ 4 files changed, 81 insertions(+), 34 deletions(-) diff --git a/Helper/Sync.php b/Helper/Sync.php index 7fbd1ca6..bf22fb1c 100644 --- a/Helper/Sync.php +++ b/Helper/Sync.php @@ -97,6 +97,10 @@ public function markEcommerceAsDeleted($relatedId, $type, $relatedDeletedId = nu { $this->chimpSyncEcommerce->markAllAsDeleted($relatedId, $type, $relatedDeletedId); } + public function markEcommorceAsNotDeleted($relatedId, $type) + { + $this->chimpSyncEcommerce->markAllAsNotDeleted($relatedId, $type); + } public function ecommerceDeleteAllByIdType($id, $type, $mailchimpStoreId) { $this->chimpSyncEcommerce->deleteAllByIdType($id, $type, $mailchimpStoreId); diff --git a/Model/Api/Product.php b/Model/Api/Product.php index 5aae20f1..768dc6c0 100644 --- a/Model/Api/Product.php +++ b/Model/Api/Product.php @@ -12,6 +12,8 @@ namespace Ebizmarts\MailChimp\Model\Api; use Ebizmarts\MailChimp\Helper\Sync as SyncHelper; +use Magento\Framework\Exception\NoSuchEntityException; + class Product { const DOWNLOADABLE = 'downloadable'; @@ -155,20 +157,42 @@ public function _sendProducts($magentoStoreId) $product = $this->_productRepository->getById($item->getId(), false, $magentoStoreId); if ($item->getMailchimpSyncModified() && $item->getMailchimpSyncDelta() && $item->getMailchimpSyncDelta() > $this->_helper->getMCMinSyncDateFlag()) { - $batchArray = array_merge($this->_buildOldProductRequest( - $product, - $this->_batchId, - $mailchimpStoreId, - $magentoStoreId - ), $batchArray); - $this->_updateProduct($mailchimpStoreId, $product->getId()); + if ($product->isSalable()) { + if (!$item->getMailchimpSyncDeleted()) { + $batchArray = array_merge($this->_buildOldProductRequest( + $product, + $this->_batchId, + $mailchimpStoreId, + $magentoStoreId + ), $batchArray); + $this->_updateProduct($mailchimpStoreId, $product->getId()); + } else { + $data = array_merge($this->_buildNewProductRequest($product, $mailchimpStoreId, $magentoStoreId), $batchArray); + $batchArray[] = $data; + $this->syncHelper->markEcommorceAsNotDeleted($product->getId(), \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT); + continue; + } + } else { + $productId = $product->getId(); + $batchArray[] = $this->deleteProduct($mailchimpStoreId, $productId,"Product not salable"); + } continue; } else { - $data = $this->_buildNewProductRequest($product, $mailchimpStoreId, $magentoStoreId); + if ($product->isSalable()) { + $data = $this->_buildNewProductRequest($product, $mailchimpStoreId, $magentoStoreId); + } else { + $this->_updateProduct( + $mailchimpStoreId, + $product->getId(), + $this->_helper->getGmtDate(), + "This product is not salable.", + 0 + ); + continue; + } } if (!empty($data)) { $batchArray[] = $data; - //update product delta $this->_updateProduct($mailchimpStoreId, $product->getId()); } else { @@ -254,33 +278,33 @@ protected function processDeletedProducts($magentoStoreId, $mailchimpStoreId) ); $collection->getSelect()->limit(self::MAX); foreach ($collection as $item) { - $data = []; - $productId = $item->getRelatedId(); - $data ['inventory_quantity'] = 0; - $body = json_encode($data, JSON_HEX_APOS|JSON_HEX_QUOT); - if ($body === false) { - $jsonError = json_last_error(); - $jsonErrorMsg = json_last_error_msg(); - $this->_helper->log(""); - $this->_helper->log("$jsonErrorMsg for product [$productId]"); - } else { - $this->_helper->modifyCounter(\Ebizmarts\MailChimp\Helper\Data::PRO_DELETED); - $data = []; - $data['method'] = "DELETE"; - $data['path'] = "/ecommerce/stores/$mailchimpStoreId/products/$productId/variants/$productId"; - $data['operation_id'] = $this->_batchId . '_' . $productId; - $data['body'] = $body; - $deletedData [] = $data; - $data['method'] = "DELETE"; - $data['path'] = "/ecommerce/stores/$mailchimpStoreId/products/$productId"; - $data['operation_id'] = $this->_batchId . '_' . $productId; - $data['body'] = $body; - $deletedData [] = $data; - $this->_updateProduct($mailchimpStoreId, $productId); + try { + $product = $this->_productRepository->getById($item->getRelatedId(), false, $magentoStoreId); + } catch (NoSuchEntityException $e) { + $productId = $item->getRelatedId(); + $deletedData[] = $this->deleteProduct($mailchimpStoreId, $productId, "Deleted product]"); } } return $deletedData; } + protected function deleteProduct($mailchimpStoreId, $productId, $error) + { + $data = []; + $data['method'] = "DELETE"; + $data['path'] = "/ecommerce/stores/$mailchimpStoreId/products/$productId"; + $data['operation_id'] = $this->_batchId . '_' . $productId; + $data['body'] = ""; + $this->_updateProduct( + $mailchimpStoreId, + $productId, + $this->_helper->getGmtDate(), + $error, + 0, + true + ); + $this->_helper->modifyCounter(\Ebizmarts\MailChimp\Helper\Data::PRO_DELETED); + return $data; + } /** * @return \Magento\Catalog\Model\ResourceModel\Product\Collection */ @@ -696,7 +720,8 @@ protected function _updateProduct( $entityId, $sync_delta = null, $sync_error = null, - $sync_modified = null + $sync_modified = null, + $deleted = null ) { $this->syncHelper->saveEcommerceData( $storeId, @@ -705,7 +730,7 @@ protected function _updateProduct( $sync_delta, $sync_error, $sync_modified, - null, + $deleted, null, \Ebizmarts\MailChimp\Helper\Data::WAITINGSYNC ); diff --git a/Model/MailChimpSyncEcommerce.php b/Model/MailChimpSyncEcommerce.php index 72880889..34f0332d 100644 --- a/Model/MailChimpSyncEcommerce.php +++ b/Model/MailChimpSyncEcommerce.php @@ -31,6 +31,11 @@ public function markAllAsDeleted($id, $type, $relatedDeletedId) $this->getResource()->markAllAsDeleted($this, $id, $type, $relatedDeletedId); return $this; } + public function markAllAsNotDeleted($id, $type) + { + $this->getResource()->markAllAsNotDeleted($this, $id, $type); + return $this; + } public function markAllAsModified($id, $type) { $this->getResource()->markAllAsModified($this, $id, $type); diff --git a/Model/ResourceModel/MailChimpSyncEcommerce.php b/Model/ResourceModel/MailChimpSyncEcommerce.php index 4fb60d64..affa3b1d 100644 --- a/Model/ResourceModel/MailChimpSyncEcommerce.php +++ b/Model/ResourceModel/MailChimpSyncEcommerce.php @@ -51,6 +51,19 @@ public function markAllAsDeleted( ); return $this; } + public function markAllAsNotDeleted( + \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $chimp, + $id, + $type + ) { + $connection = $this->getConnection(); + $connection->update( + $this->getTable('mailchimp_sync_ecommerce'), + ['mailchimp_sync_deleted'=>0, 'mailchimp_sync_error' => null,'mailchimp_sync_modified'=>0,'mailchimp_sent'=>\Ebizmarts\MailChimp\Helper\Data::WAITINGSYNC], + ['related_id = ?'=> $id,'type = ?'=>$type] + ); + return $this; + } public function markAllAsModified(\Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $chimp, $id, $type) { $connection = $this->getConnection(); From 50f1cfc362b4a5806bb09643091716b8cd832d69 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Thu, 26 Sep 2024 08:56:54 -0300 Subject: [PATCH 2/3] closes #2003 for magento 2.3.6-p1 --- Model/Api/Product.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Model/Api/Product.php b/Model/Api/Product.php index 768dc6c0..1076d663 100644 --- a/Model/Api/Product.php +++ b/Model/Api/Product.php @@ -186,7 +186,9 @@ public function _sendProducts($magentoStoreId) $product->getId(), $this->_helper->getGmtDate(), "This product is not salable.", - 0 + 0, + true, + true ); continue; } @@ -721,8 +723,14 @@ protected function _updateProduct( $sync_delta = null, $sync_error = null, $sync_modified = null, - $deleted = null + $deleted = null, + $forcedNoWaiting = false ) { + if ($forcedNoWaiting) { + $sent = \Ebizmarts\MailChimp\Helper\Data::SYNCED; + } else { + $sent = \Ebizmarts\MailChimp\Helper\Data::WAITINGSYNC; + } $this->syncHelper->saveEcommerceData( $storeId, $entityId, @@ -732,7 +740,7 @@ protected function _updateProduct( $sync_modified, $deleted, null, - \Ebizmarts\MailChimp\Helper\Data::WAITINGSYNC + $sent ); } } From 697b76866e3dc881db2627e0bc4b4eaccc774686 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Fri, 27 Sep 2024 12:29:33 -0300 Subject: [PATCH 3/3] change function name #2003 for magento 2.3.6-p1 --- Helper/Sync.php | 2 +- Model/MailChimpSyncEcommerce.php | 4 ++-- Model/ResourceModel/MailChimpSyncEcommerce.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Helper/Sync.php b/Helper/Sync.php index bf22fb1c..9b85fee0 100644 --- a/Helper/Sync.php +++ b/Helper/Sync.php @@ -99,7 +99,7 @@ public function markEcommerceAsDeleted($relatedId, $type, $relatedDeletedId = nu } public function markEcommorceAsNotDeleted($relatedId, $type) { - $this->chimpSyncEcommerce->markAllAsNotDeleted($relatedId, $type); + $this->chimpSyncEcommerce->markEcommorceAsNotDeleted($relatedId, $type); } public function ecommerceDeleteAllByIdType($id, $type, $mailchimpStoreId) { diff --git a/Model/MailChimpSyncEcommerce.php b/Model/MailChimpSyncEcommerce.php index 34f0332d..7a960246 100644 --- a/Model/MailChimpSyncEcommerce.php +++ b/Model/MailChimpSyncEcommerce.php @@ -31,9 +31,9 @@ public function markAllAsDeleted($id, $type, $relatedDeletedId) $this->getResource()->markAllAsDeleted($this, $id, $type, $relatedDeletedId); return $this; } - public function markAllAsNotDeleted($id, $type) + public function markEcommorceAsNotDeleted($id, $type) { - $this->getResource()->markAllAsNotDeleted($this, $id, $type); + $this->getResource()->markEcommorceAsNotDeleted($this, $id, $type); return $this; } public function markAllAsModified($id, $type) diff --git a/Model/ResourceModel/MailChimpSyncEcommerce.php b/Model/ResourceModel/MailChimpSyncEcommerce.php index affa3b1d..cbca7127 100644 --- a/Model/ResourceModel/MailChimpSyncEcommerce.php +++ b/Model/ResourceModel/MailChimpSyncEcommerce.php @@ -51,7 +51,7 @@ public function markAllAsDeleted( ); return $this; } - public function markAllAsNotDeleted( + public function markEcommorceAsNotDeleted( \Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce $chimp, $id, $type