Skip to content

Commit

Permalink
Merge pull request #2007 from mailchimp/Issue2003-2.3.6-p1
Browse files Browse the repository at this point in the history
Issue2003 2.3.6 p1
  • Loading branch information
gonzaloebiz authored Sep 30, 2024
2 parents fdcb13c + 697b768 commit 4f2c694
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 35 deletions.
4 changes: 4 additions & 0 deletions Helper/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ public function markEcommerceAsDeleted($relatedId, $type, $relatedDeletedId = nu
{
$this->chimpSyncEcommerce->markAllAsDeleted($relatedId, $type, $relatedDeletedId);
}
public function markEcommorceAsNotDeleted($relatedId, $type)
{
$this->chimpSyncEcommerce->markEcommorceAsNotDeleted($relatedId, $type);
}
public function ecommerceDeleteAllByIdType($id, $type, $mailchimpStoreId)
{
$this->chimpSyncEcommerce->deleteAllByIdType($id, $type, $mailchimpStoreId);
Expand Down
103 changes: 68 additions & 35 deletions Model/Api/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -155,20 +157,44 @@ 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,
true,
true
);
continue;
}
}
if (!empty($data)) {
$batchArray[] = $data;

//update product delta
$this->_updateProduct($mailchimpStoreId, $product->getId());
} else {
Expand Down Expand Up @@ -254,33 +280,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
*/
Expand Down Expand Up @@ -696,18 +722,25 @@ protected function _updateProduct(
$entityId,
$sync_delta = null,
$sync_error = null,
$sync_modified = null
$sync_modified = 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,
\Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT,
$sync_delta,
$sync_error,
$sync_modified,
$deleted,
null,
null,
\Ebizmarts\MailChimp\Helper\Data::WAITINGSYNC
$sent
);
}
}
5 changes: 5 additions & 0 deletions Model/MailChimpSyncEcommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public function markAllAsDeleted($id, $type, $relatedDeletedId)
$this->getResource()->markAllAsDeleted($this, $id, $type, $relatedDeletedId);
return $this;
}
public function markEcommorceAsNotDeleted($id, $type)
{
$this->getResource()->markEcommorceAsNotDeleted($this, $id, $type);
return $this;
}
public function markAllAsModified($id, $type)
{
$this->getResource()->markAllAsModified($this, $id, $type);
Expand Down
13 changes: 13 additions & 0 deletions Model/ResourceModel/MailChimpSyncEcommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ public function markAllAsDeleted(
);
return $this;
}
public function markEcommorceAsNotDeleted(
\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();
Expand Down

0 comments on commit 4f2c694

Please sign in to comment.