Skip to content

Commit

Permalink
Merge pull request #1768 from mailchimp/Issue1758-2.2
Browse files Browse the repository at this point in the history
closes #1758 for magento 2.2
  • Loading branch information
gonzaloebiz authored Aug 17, 2023
2 parents 7cd935e + 221d371 commit 49ca04c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 72 deletions.
23 changes: 1 addition & 22 deletions Helper/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Exception\ValidatorException;
use Magento\Sales\Model\OrderFactory;
use Magento\Catalog\Model\ProductFactory;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
use Ebizmarts\MailChimp\Model\MailChimpSyncEcommerceFactory;
use Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce;
Expand Down Expand Up @@ -34,10 +33,6 @@ class Sync extends AbstractHelper
* @var OrderCollectionFactory
*/
private $orderCollectionFactory;
/**
* @var ProductFactory
*/
private $productFactory;

/**
* @param Context $context
Expand All @@ -46,23 +41,20 @@ class Sync extends AbstractHelper
* @param MailChimpSyncEcommerce $chimpSyncEcommerce
* @param OrderFactory $orderFactory
* @param OrderCollectionFactory $orderCollectionFactory
* @param ProductFactory $productFactory
*/
public function __construct(
Context $context,
MailChimpSyncEcommerceFactory $chimpSyncEcommerceFactory,
MailChimpErrors $mailChimpErrors,
MailChimpSyncEcommerce $chimpSyncEcommerce,
OrderFactory $orderFactory,
OrderCollectionFactory $orderCollectionFactory,
ProductFactory $productFactory
OrderCollectionFactory $orderCollectionFactory
) {
$this->chimpSyncEcommerceFactory = $chimpSyncEcommerceFactory;
$this->mailChimpErrors = $mailChimpErrors;
$this->chimpSyncEcommerce = $chimpSyncEcommerce;
$this->orderFactory = $orderFactory;
$this->orderCollectionFactory = $orderCollectionFactory;
$this->productFactory = $productFactory;
parent::__construct($context);
}
public function saveEcommerceData(
Expand Down Expand Up @@ -119,19 +111,6 @@ public function saveEcommerceData(
$order->save();
}
break;
case \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT :
if ($sent || $error) {
$product = $this->productFactory->create()->load($entityId);
$product->setSync(true);
if ($sent) {
$product->setMailchimpSent($sent);
}
if ($error) {
$product->setMailchimpSyncError($error);
}
$product->save();
}
break;
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions Observer/Adminhtml/Product/SaveAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,20 @@ public function execute(\Magento\Framework\Event\Observer $observer)
\Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE,
$product->getStoreId()
);
$sync = $product->getSync();
if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) {
$parents = $this->configurable->getParentIdsByChild($product->getId());
if (is_array($parents)) {
foreach ($parents as $parentid) {
$this->_updateProduct($parentid, $sync);
$this->_updateProduct($parentid);
}
} elseif ($parents) {
$this->_updateProduct($parents, $sync);
$this->_updateProduct($parents);
}
}
$this->_updateProduct($product->getId(), $sync);
$this->_updateProduct($product->getId());
}
protected function _updateProduct($entityId ,$sync)
protected function _updateProduct($entityId)
{
if ($sync) {
$this->syncHelper->markRegisterAsModified($entityId, \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT);
}
$this->syncHelper->markRegisterAsModified($entityId, \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT);
}
}
20 changes: 0 additions & 20 deletions Observer/Adminhtml/Product/SaveBefore.php

This file was deleted.

11 changes: 11 additions & 0 deletions Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,5 +720,16 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
]
);
}
if (version_compare($context->getVersion(), '101.2.53') < 0) {
$salesConnection->dropColumn(
$setup->getTable('catalog_product_entity'),
'mailchimp_sync_error'
);
$salesConnection->dropColumn(
$setup->getTable('catalog_product_entity'),
'mailchimp_sent',
);
}

}
}
52 changes: 33 additions & 19 deletions Ui/Component/Listing/Column/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncEcommerce\CollectionFactory as SyncCollectionFactory;
use Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncEcommerce\Collection as SyncCollection;
use Ebizmarts\MailChimp\Helper\Data as MailChimpHelper;


class Products extends Column
{
Expand Down Expand Up @@ -43,19 +47,8 @@ class Products extends Column
* @var ProductCollectionFactory
*/
private $productCollectionFactory;
private $syncCollectionFactory;

/**
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param ProductFactory $productFactory
* @param ProductCollectionFactory $productCollectionFactory
* @param RequestInterface $requestInterface
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param \Magento\Framework\View\Asset\Repository $assetRepository
* @param \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory $mailChimpErrorsFactory
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
Expand All @@ -65,6 +58,7 @@ public function __construct(
\Ebizmarts\MailChimp\Helper\Data $helper,
\Magento\Framework\View\Asset\Repository $assetRepository,
\Ebizmarts\MailChimp\Model\MailChimpErrorsFactory $mailChimpErrorsFactory,
SyncCollectionFactory $syncCollectionFactory,
array $components = [],
array $data = [])
{
Expand All @@ -74,6 +68,7 @@ public function __construct(
$this->_assetRepository = $assetRepository;
$this->_mailChimpErrorsFactory = $mailChimpErrorsFactory;
$this->productCollectionFactory = $productCollectionFactory;
$this->syncCollectionFactory = $syncCollectionFactory;
parent::__construct($context, $uiComponentFactory, $components, $data);
}

Expand All @@ -83,12 +78,18 @@ public function prepareDataSource(array $dataSource)
$productsMap = $this->getProductsByEntityIds(
$this->getProductsIds($dataSource)
);
$syncMap = $this->getSyncDataByEntityIds($this->getProductsIds($dataSource));
foreach ($dataSource['data']['items'] as & $item) {
/**
* @var $product \Magento\Catalog\Model\Product
*/
$product = $productsMap[$item['entity_id']]
?? $this->_productFactory->create()->load($item['entity_id']); // Backwards Compatibility
if (key_exists($item['entity_id'],$syncMap)) {
$sync = $syncMap[$item['entity_id']]->getMailchimpSent();
} else {
$sync = MailChimpHelper::NEVERSYNC;
}
$params = ['_secure' => $this->_requestInterface->isSecure()];
$alt = '';
$url = '';
Expand All @@ -97,30 +98,29 @@ public function prepareDataSource(array $dataSource)
$url = '';
$text = '';
if ($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_ACTIVE, $product->getStoreId())) {
$sync = $item['mailchimp_sent'];
switch ($sync) {
case \Ebizmarts\MailChimp\Helper\Data::NEVERSYNC:
case MailChimpHelper::NEVERSYNC:
$url = $this->_assetRepository->getUrlWithParams(
'Ebizmarts_MailChimp::images/no.png',
$params
);
$text = __('Syncing');
break;
case \Ebizmarts\MailChimp\Helper\Data::SYNCED:
case MailChimpHelper::SYNCED:
$url = $this->_assetRepository->getUrlWithParams(
'Ebizmarts_MailChimp::images/yes.png',
$params
);
$text = __('Synced');
break;
case \Ebizmarts\MailChimp\Helper\Data::WAITINGSYNC:
case MailChimpHelper::WAITINGSYNC:
$url = $this->_assetRepository->getUrlWithParams(
'Ebizmarts_MailChimp::images/waiting.png',
$params
);
$text = __('Waiting');
break;
case \Ebizmarts\MailChimp\Helper\Data::SYNCERROR:
case MailChimpHelper::SYNCERROR:
$url = $this->_assetRepository->getUrlWithParams(
'Ebizmarts_MailChimp::images/error.png',
$params
Expand All @@ -131,14 +131,14 @@ public function prepareDataSource(array $dataSource)
$alt = $orderError->getErrors();
}
break;
case \Ebizmarts\MailChimp\Helper\Data::NEEDTORESYNC:
case MailChimpHelper::NEEDTORESYNC:
$url = $this->_assetRepository->getUrlWithParams(
'Ebizmarts_MailChimp::images/resync.png',
$params
);
$text = __('Resyncing');
break;
case \Ebizmarts\MailChimp\Helper\Data::NOTSYNCED:
case MailChimpHelper::NOTSYNCED:
$url = $this->_assetRepository->getUrlWithParams(
'Ebizmarts_MailChimp::images/never.png',
$params
Expand Down Expand Up @@ -201,4 +201,18 @@ private function getProductsIds(array $dataSource)

return array_filter(array_unique(array_column($dataSource['data']['items'], 'entity_id')));
}
private function getSyncDataByEntityIds(array $productIds)
{
$syncMap = [];
/**
* @var SyncCollection $syncCollection
*/
$syncCollection = $this->syncCollectionFactory->create();
$syncCollection->addFieldToFilter('related_id', ['in' => $productIds]);
$syncCollection->addFieldToFilter('type', ['eq' => MailChimpHelper::IS_PRODUCT]);
foreach($syncCollection as $item) {
$syncMap[$item->getRelatedId()] = $item;
}
return $syncMap;
}
}
3 changes: 0 additions & 3 deletions etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
<event name="catalog_product_save_after">
<observer name="mailchimp_product_save_after" instance="\Ebizmarts\MailChimp\Observer\Adminhtml\Product\SaveAfter" />
</event>
<event name="catalog_product_save_before">
<observer name="mailchimp_product_save_before" instance="\Ebizmarts\MailChimp\Observer\Adminhtml\Product\SaveBefore" />
</event>
<event name="newsletter_subscriber_save_after">
<observer name="mailchimp_newsletter_subscriber_save_after" instance="\Ebizmarts\MailChimp\Observer\Subscriber\SaveAfter" />
</event>
Expand Down

0 comments on commit 49ca04c

Please sign in to comment.