Skip to content

Commit

Permalink
Merge pull request #1774 from mailchimp/Issue1772-2.2
Browse files Browse the repository at this point in the history
 closes #1772 for magento 2.2
  • Loading branch information
gonzaloebiz authored Aug 22, 2023
2 parents 49ca04c + 6776b61 commit 30ea7ae
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 102 deletions.
22 changes: 0 additions & 22 deletions Helper/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Exception\ValidatorException;
use Magento\Sales\Model\OrderFactory;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
use Ebizmarts\MailChimp\Model\MailChimpSyncEcommerceFactory;
use Ebizmarts\MailChimp\Model\MailChimpSyncEcommerce;
Expand All @@ -25,10 +24,6 @@ class Sync extends AbstractHelper
* @var MailChimpSyncEcommerce
*/
private $chimpSyncEcommerce;
/**
* @var OrderFactory
*/
private $orderFactory;
/**
* @var OrderCollectionFactory
*/
Expand All @@ -39,21 +34,18 @@ class Sync extends AbstractHelper
* @param MailChimpSyncEcommerceFactory $chimpSyncEcommerceFactory
* @param MailChimpErrors $mailChimpErrors
* @param MailChimpSyncEcommerce $chimpSyncEcommerce
* @param OrderFactory $orderFactory
* @param OrderCollectionFactory $orderCollectionFactory
*/
public function __construct(
Context $context,
MailChimpSyncEcommerceFactory $chimpSyncEcommerceFactory,
MailChimpErrors $mailChimpErrors,
MailChimpSyncEcommerce $chimpSyncEcommerce,
OrderFactory $orderFactory,
OrderCollectionFactory $orderCollectionFactory
) {
$this->chimpSyncEcommerceFactory = $chimpSyncEcommerceFactory;
$this->mailChimpErrors = $mailChimpErrors;
$this->chimpSyncEcommerce = $chimpSyncEcommerce;
$this->orderFactory = $orderFactory;
$this->orderCollectionFactory = $orderCollectionFactory;
parent::__construct($context);
}
Expand Down Expand Up @@ -98,20 +90,6 @@ public function saveEcommerceData(
}
$chimpSyncEcommerce->getResource()->save($chimpSyncEcommerce);
}
switch ($type) {
case \Ebizmarts\MailChimp\Helper\Data::IS_ORDER :
if ($sent || $error) {
$order = $this->orderFactory->create()->loadByAttribute('entity_id', $entityId);
if ($sent) {
$order->setMailchimpSent($sent);
}
if ($error) {
$order->setMailchimpSyncError($error);
}
$order->save();
}
break;
}
}
}
public function getChimpSyncEcommerce($storeId, $id, $type)
Expand Down
42 changes: 0 additions & 42 deletions Setup/UpgradeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,47 +183,5 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
$config->getResource()->delete($config);
}
}
if (version_compare($context->getVersion(), '101.2.53') < 0) {
$setup->startSetup();
$connection = $this->_resource->getConnectionByName('default');
try {
$tableOrders = $setup->getTable('sales_order');
$tableEcommerce = $setup->getTable('mailchimp_sync_ecommerce');
$query = "UPDATE `$tableOrders` as A ";
$query .= "INNER JOIN `$tableEcommerce` as B ON A.`entity_id` = B.`related_id` ";
$query .= "SET A.`mailchimp_sync_error` = B.`mailchimp_sync_error`, A.`mailchimp_sent` = B.`mailchimp_sent` ";
$query .= "WHERE B.`type` = 'ORD'";
$connection->query($query);

} catch(\Exception $e) {
$this->_helper->log($e->getMessage());
throw new \Exception($e->getMessage());
}
try {
$tableOrdersGrid = $setup->getTable('sales_order_grid');
$tableEcommerce = $setup->getTable('mailchimp_sync_ecommerce');
$query = "UPDATE `$tableOrdersGrid` as A ";
$query .= "INNER JOIN `$tableEcommerce` as B ON A.`entity_id` = B.`related_id` ";
$query .= "SET A.`mailchimp_sync_error` = B.`mailchimp_sync_error`, A.`mailchimp_sent` = B.`mailchimp_sent` ";
$query .= "WHERE B.`type` = 'ORD'";
$connection->query($query);

} catch(\Exception $e) {
$this->_helper->log($e->getMessage());
throw new \Exception($e->getMessage());
}
try {
$tableProducts = $setup->getTable('catalog_product_entity');
$tableEcommerce = $setup->getTable('mailchimp_sync_ecommerce');
$query = "UPDATE `$tableProducts` as A ";
$query .= "INNER JOIN `$tableEcommerce` as B ON A.`entity_id` = B.`related_id` ";
$query .= "SET A.`mailchimp_sync_error` = B.`mailchimp_sync_error`, A.`mailchimp_sent` = B.`mailchimp_sent` ";
$query .= "WHERE B.`type` = 'PRO'";
$connection->query($query);
} catch(\Exception $e) {
$this->_helper->log($e->getMessage());
throw new \Exception($e->getMessage());
}
}
}
}
20 changes: 18 additions & 2 deletions Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,14 +720,30 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con
]
);
}
if (version_compare($context->getVersion(), '101.2.53') < 0) {
if (version_compare($context->getVersion(), '101.2.54') < 0) {
$salesConnection->dropColumn(
$setup->getTable('catalog_product_entity'),
'mailchimp_sync_error'
);
$salesConnection->dropColumn(
$setup->getTable('catalog_product_entity'),
'mailchimp_sent',
'mailchimp_sent'
);
$salesConnection->dropColumn(
$setup->getTable('sales_order'),
'mailchimp_sync_error'
);
$salesConnection->dropColumn(
$setup->getTable('sales_order'),
'mailchimp_sent'
);
$salesConnection->dropColumn(
$setup->getTable('sales_order_grid'),
'mailchimp_sync_error'
);
$salesConnection->dropColumn(
$setup->getTable('sales_order_grid'),
'mailchimp_sent'
);
}

Expand Down
114 changes: 78 additions & 36 deletions Ui/Component/Listing/Column/Monkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\UrlInterface;
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 Monkey extends Column
Expand All @@ -39,7 +42,7 @@ class Monkey extends Column
*/
protected $_requestInterfase;
/**
* @var \Ebizmarts\MailChimp\Helper\Data
* @var MailChimpHelper
*/
protected $_helper;
/**
Expand All @@ -58,8 +61,32 @@ class Monkey extends Column
* @var UrlInterface
*/
protected $urlBuilder;
/**
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
*/
private $orderCollectionFactory;
/**
* @var SyncCollectionFactory
*/
private $syncCollectionFactory;

/**
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param OrderRepositoryInterface $orderRepository
* @param \Magento\Framework\View\Asset\Repository $assetRepository
* @param \Magento\Framework\App\RequestInterface $requestInterface
* @param SearchCriteriaBuilder $criteria
* @param MailChimpHelper $helper
* @param SyncCollectionFactory $syncCommerceCF
* @param \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory $mailChimpErrorsFactory
* @param \Magento\Sales\Model\OrderFactory $orderFactory
* @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
* @param UrlInterface $urlBuilder
* @param SyncCollectionFactory $syncCollectionFactory
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
Expand All @@ -73,6 +100,7 @@ public function __construct(
\Magento\Sales\Model\OrderFactory $orderFactory,
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
UrlInterface $urlBuilder,
SyncCollectionFactory $syncCollectionFactory,
array $components = [],
array $data = []
) {
Expand All @@ -86,20 +114,26 @@ public function __construct(
$this->orderCollectionFactory = $orderCollectionFactory;
$this->_mailChimpErrorsFactory = $mailChimpErrorsFactory;
$this->urlBuilder = $urlBuilder;
$this->syncCollectionFactory = $syncCollectionFactory;
parent::__construct($context, $uiComponentFactory, $components, $data);
}

public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
$orderMap = $this->getOrderDataForIncrementIds(
$this->getOrderIncrementIds($dataSource)
);
$ordersIds = $this->getOrdersIds($dataSource);
$orderMap = $this->getOrderDataByOrderIds($ordersIds);
$syncMap = $this->getSyncDataByOrderIds($ordersIds);
foreach ($dataSource['data']['items'] as & $item) {
$status = $item['mailchimp_flag'];
$orderId = $item['increment_id'];
$sync = $item['mailchimp_sent'];
$error = $item['mailchimp_sync_error'];
$orderId = $item['entity_id'];
if (key_exists($item['entity_id'],$syncMap)) {
$sync = $syncMap[$item['entity_id']]->getMailchimpSent();
$syncError = $syncMap[$item['entity_id']]->getMailchimpSyncError();
} else {
$sync = MailChimpHelper::NEVERSYNC;
$syncError = '';
}

$order = $orderMap[$orderId]
?? $this->_orderFactory->create()->loadByIncrementId($orderId); // Backwards Compatibility
Expand All @@ -109,29 +143,29 @@ public function prepareDataSource(array $dataSource)
if ($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_ACTIVE, $storeId)) {
$alt = '';
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');
$menu = true;
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 @@ -142,21 +176,21 @@ 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');
$menu = true;
break;
case \Ebizmarts\MailChimp\Helper\Data::NOTSYNCED:
case MailChimpHelper::NOTSYNCED:
$url = $this->_assetRepository->getUrlWithParams(
'Ebizmarts_MailChimp::images/never.png',
$params
);
$text = __('With error');
$alt = $item['mailchimp_sync_error'];
$alt = $syncError;
break;
default:
$url ='';
Expand Down Expand Up @@ -207,43 +241,51 @@ private function _getError($orderId, $storeId)
return $error->getByStoreIdType($storeId, $orderId, \Ebizmarts\MailChimp\Helper\Data::IS_ORDER);
}
/**
* Extract Order Increment IDs for a given DataSource
*
* @param array $dataSource
* @return array
*/
private function getOrderIncrementIds(array $dataSource): array
{
if (!isset($dataSource['data']['items'])) {
return [];
}

return array_filter(array_unique(array_column($dataSource['data']['items'], 'increment_id')));
}

/**
* @param array $incrementIds
* @param array $orderIds
* @return OrderInterface[]
*/
private function getOrderDataForIncrementIds(array $incrementIds): array
private function getOrderDataByOrderIds(array $orderIds): array
{
if (empty($incrementIds)) {
if (empty($orderIds)) {
return [];
}

$orderCollection = $this->orderCollectionFactory->create();
$orderCollection->getSelect()->columns(['entity_id', 'increment_id', 'store_id']);
$orderCollection->getSelect()->columns(['entity_id','store_id']);
$orderCollection->addAttributeToFilter(
'increment_id',
['in' => $incrementIds]
'entity_id',
['in' => $orderIds]
);

$ordersMap = [];
/** @var OrderInterface $order */
foreach ($orderCollection->getItems() as $order) {
$ordersMap[$order->getIncrementId()] = $order;
$ordersMap[$order->getEntityId()] = $order;
}

return $ordersMap;
}
private function getOrdersIds(array $dataSource)
{
if (!isset($dataSource['data']['items'])) {
return [];
}

return array_filter(array_unique(array_column($dataSource['data']['items'], 'entity_id')));
}

private function getSyncDataByOrderIds(array $OrderIds)
{
$syncMap = [];
/**
* @var SyncCollection $syncCollection
*/
$syncCollection = $this->syncCollectionFactory->create();
$syncCollection->addFieldToFilter('related_id', ['in' => $OrderIds]);
$syncCollection->addFieldToFilter('type', ['eq' => MailChimpHelper::IS_ORDER]);
foreach($syncCollection as $item) {
$syncMap[$item->getRelatedId()] = $item;
}
return $syncMap;
}
}

0 comments on commit 30ea7ae

Please sign in to comment.