diff --git a/Ui/Component/Listing/Column/Monkey.php b/Ui/Component/Listing/Column/Monkey.php index e17c3bef..44f11537 100644 --- a/Ui/Component/Listing/Column/Monkey.php +++ b/Ui/Component/Listing/Column/Monkey.php @@ -12,12 +12,13 @@ */ namespace Ebizmarts\MailChimp\Ui\Component\Listing\Column; -use \Magento\Sales\Api\OrderRepositoryInterface; -use \Magento\Framework\View\Element\UiComponent\ContextInterface; -use \Magento\Framework\View\Element\UiComponentFactory; -use \Magento\Ui\Component\Listing\Columns\Column; -use \Magento\Framework\Api\SearchCriteriaBuilder; -use \Magento\Store\Model\StoreManagerInterface; +use Ebizmarts\MailChimp\Helper\Data as Helper; +use Magento\Framework\Api\SearchCriteriaBuilder; +use Magento\Framework\View\Element\UiComponent\ContextInterface; +use Magento\Framework\View\Element\UiComponentFactory; +use Magento\Sales\Api\OrderRepositoryInterface; +use Magento\Sales\Model\ResourceModel\Order\Collection as OrderCollection; +use Magento\Ui\Component\Listing\Columns\Column; class Monkey extends Column { @@ -53,6 +54,10 @@ class Monkey extends Column * @var \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory */ protected $_mailChimpErrorsFactory; + /** + * @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory + */ + protected $_orderCollectionFactory; /** * Monkey constructor. @@ -80,10 +85,11 @@ public function __construct( \Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncEcommerce\CollectionFactory $syncCommerceCF, \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory $mailChimpErrorsFactory, \Magento\Sales\Model\OrderFactory $orderFactory, + \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory, array $components = [], array $data = [] ) { - + $this->_orderRepository = $orderRepository; $this->_searchCriteria = $criteria; $this->_assetRepository = $assetRepository; @@ -91,97 +97,120 @@ public function __construct( $this->_helper = $helper; $this->_syncCommerceCF = $syncCommerceCF; $this->_orderFactory = $orderFactory; + $this->_orderCollectionFactory = $orderCollectionFactory; $this->_mailChimpErrorsFactory = $mailChimpErrorsFactory; parent::__construct($context, $uiComponentFactory, $components, $data); } public function prepareDataSource(array $dataSource) { - if (isset($dataSource['data']['items'])) { - foreach ($dataSource['data']['items'] as & $item) { - $status = $item['mailchimp_flag']; - $order = $this->_orderFactory->create()->loadByIncrementId($item['increment_id']); - $params = ['_secure' => $this->_requestInterfase->isSecure()]; - if ($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_ACTIVE, $order->getStoreId())) { - $mailchimpStoreId = $this->_helper->getConfigValue( - \Ebizmarts\MailChimp\Helper\Data::XML_MAILCHIMP_STORE, - $order->getStoreId() - ); - $syncData = $this->_helper->getChimpSyncEcommerce( - $mailchimpStoreId, - $order->getId(), - \Ebizmarts\MailChimp\Helper\Data::IS_ORDER - ); - $alt = ''; - if (!$syncData || $syncData->getMailchimpStoreId() != $mailchimpStoreId || - $syncData->getRelatedId() != $order->getId() || - $syncData->getType() != \Ebizmarts\MailChimp\Helper\Data::IS_ORDER) { + if (!isset($dataSource['data']['items'])) { + return $dataSource; + } + + $orderIds = array_map(function ($item) { + return $item['entity_id']; + }, $dataSource['data']['items']); + + /** @var OrderCollection $orderCollection */ + $orderCollection = $this->_orderCollectionFactory->create() + ->removeAllFieldsFromSelect() + ->addFieldToSelect('entity_id') + ->addFieldToSelect('store_id') + ->addFieldToFilter('entity_id', ['in' => $orderIds]) + ->load(); + + foreach ($dataSource['data']['items'] as & $item) { + $status = $item['mailchimp_flag']; + $order = $orderCollection->getItemById($item['entity_id']); + $params = ['_secure' => $this->_requestInterfase->isSecure()]; + + if ($order === null) { + continue; + } + + if (!$this->_helper->getConfigValue(Helper::XML_PATH_ACTIVE, $order->getStoreId())) { + continue; + } + + $mailchimpStoreId = $this->_helper->getConfigValue( + Helper::XML_MAILCHIMP_STORE, + $order->getStoreId() + ); + $syncData = $this->_helper->getChimpSyncEcommerce( + $mailchimpStoreId, + $order->getId(), + Helper::IS_ORDER + ); + $alt = ''; + if (!$syncData || $syncData->getMailchimpStoreId() != $mailchimpStoreId || + $syncData->getRelatedId() != $order->getId() || + $syncData->getType() != Helper::IS_ORDER) { + $url = $this->_assetRepository->getUrlWithParams( + 'Ebizmarts_MailChimp::images/no.png', + $params + ); + $text = __('Syncing'); + } else { + $sync = $syncData->getMailchimpSent(); + switch ($sync) { + case Helper::SYNCED: + $url = $this->_assetRepository->getUrlWithParams( + 'Ebizmarts_MailChimp::images/yes.png', + $params + ); + $text = __('Synced'); + break; + case Helper::WAITINGSYNC: $url = $this->_assetRepository->getUrlWithParams( - 'Ebizmarts_MailChimp::images/no.png', + 'Ebizmarts_MailChimp::images/waiting.png', $params ); - $text = __('Syncing'); - } else { - $sync = $syncData->getMailchimpSent(); - switch ($sync) { - case \Ebizmarts\MailChimp\Helper\Data::SYNCED: - $url = $this->_assetRepository->getUrlWithParams( - 'Ebizmarts_MailChimp::images/yes.png', - $params - ); - $text = __('Synced'); - break; - case \Ebizmarts\MailChimp\Helper\Data::WAITINGSYNC: - $url = $this->_assetRepository->getUrlWithParams( - 'Ebizmarts_MailChimp::images/waiting.png', - $params - ); - $text = __('Waiting'); - break; - case \Ebizmarts\MailChimp\Helper\Data::SYNCERROR: - $url = $this->_assetRepository->getUrlWithParams( - 'Ebizmarts_MailChimp::images/error.png', - $params - ); - $text = __('Error'); - $orderError = $this->_getError($order->getId(), $order->getStoreId()); - if ($orderError) { - $alt = $orderError->getErrors(); - } - break; - case \Ebizmarts\MailChimp\Helper\Data::NEEDTORESYNC: - $url = $this->_assetRepository->getUrlWithParams( - 'Ebizmarts_MailChimp::images/resync.png', - $params - ); - $text = __('Resyncing'); - break; - case \Ebizmarts\MailChimp\Helper\Data::NOTSYNCED: - $url = $this->_assetRepository->getUrlWithParams( - 'Ebizmarts_MailChimp::images/never.png', - $params - ); - $text = __('With error'); - $alt = $syncData->getMailchimpSyncError(); - break; - default: - $url =''; - $text = ''; + $text = __('Waiting'); + break; + case Helper::SYNCERROR: + $url = $this->_assetRepository->getUrlWithParams( + 'Ebizmarts_MailChimp::images/error.png', + $params + ); + $text = __('Error'); + $orderError = $this->_getError($order->getId(), $order->getStoreId()); + if ($orderError) { + $alt = $orderError->getErrors(); } - } - $item['mailchimp_sync'] = - "