diff --git a/Helper/Sync.php b/Helper/Sync.php index 58d39671..65c26842 100644 --- a/Helper/Sync.php +++ b/Helper/Sync.php @@ -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; @@ -34,10 +33,6 @@ class Sync extends AbstractHelper * @var OrderCollectionFactory */ private $orderCollectionFactory; - /** - * @var ProductFactory - */ - private $productFactory; /** * @param Context $context @@ -46,7 +41,6 @@ class Sync extends AbstractHelper * @param MailChimpSyncEcommerce $chimpSyncEcommerce * @param OrderFactory $orderFactory * @param OrderCollectionFactory $orderCollectionFactory - * @param ProductFactory $productFactory */ public function __construct( Context $context, @@ -54,15 +48,13 @@ public function __construct( 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( @@ -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; } } } diff --git a/Observer/Adminhtml/Product/SaveAfter.php b/Observer/Adminhtml/Product/SaveAfter.php index 96857804..8bce5a70 100644 --- a/Observer/Adminhtml/Product/SaveAfter.php +++ b/Observer/Adminhtml/Product/SaveAfter.php @@ -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); } } diff --git a/Observer/Adminhtml/Product/SaveBefore.php b/Observer/Adminhtml/Product/SaveBefore.php deleted file mode 100644 index d879027c..00000000 --- a/Observer/Adminhtml/Product/SaveBefore.php +++ /dev/null @@ -1,20 +0,0 @@ -getProduct(); - $sync = $product->getSync(); - if (!$sync&&$product->getMailchimpSent()!=MailchimpHelper::NEVERSYNC) { - $product->setMailchimpSent(MailchimpHelper::NEEDTORESYNC); - $product->setMailchimpSyncError(null); - } - return $this; - } -} diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index c822242b..2bd14d28 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -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', + ); + } + } } diff --git a/Ui/Component/Listing/Column/Products.php b/Ui/Component/Listing/Column/Products.php index 23001fff..812edb3d 100644 --- a/Ui/Component/Listing/Column/Products.php +++ b/Ui/Component/Listing/Column/Products.php @@ -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 { @@ -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, @@ -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 = []) { @@ -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); } @@ -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 = ''; @@ -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 @@ -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 @@ -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; + } } diff --git a/etc/events.xml b/etc/events.xml index 2f72a90b..1e8c1ae3 100644 --- a/etc/events.xml +++ b/etc/events.xml @@ -23,9 +23,6 @@ - - -