Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch orders using collection #1074

Open
wants to merge 2 commits into
base: develop-2.3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 111 additions & 82 deletions Ui/Component/Listing/Column/Monkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -80,108 +85,132 @@ 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;
$this->_requestInterfase= $requestInterface;
$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'] =
"<div style='width: 50%;margin: 0 auto;text-align: center'><img src='".$url."' style='border: none; width: 5rem; text-align: center; max-width: 100%' title='$alt' />$text</div>";
if ($status) {
$url = $this->_assetRepository->getUrlWithParams('Ebizmarts_MailChimp::images/freddie.png', $params);
$item['mailchimp_status'] =
"<div style='width: 50%;margin: 0 auto'><img src='".$url."' style='border: none; width: 5rem; text-align: center; max-width: 100%'/></div>";
}
break;
case Helper::NEEDTORESYNC:
$url = $this->_assetRepository->getUrlWithParams(
'Ebizmarts_MailChimp::images/resync.png',
$params
);
$text = __('Resyncing');
break;
case Helper::NOTSYNCED:
$url = $this->_assetRepository->getUrlWithParams(
'Ebizmarts_MailChimp::images/never.png',
$params
);
$text = __('With error');
$alt = $syncData->getMailchimpSyncError();
break;
default:
$url ='';
$text = '';
}
}
$item['mailchimp_sync'] =
"<div style='width: 50%;margin: 0 auto;text-align: center'><img src='".$url."' style='border: none; width: 5rem; text-align: center; max-width: 100%' title='$alt' />$text</div>";
if ($status) {
$url = $this->_assetRepository->getUrlWithParams('Ebizmarts_MailChimp::images/freddie.png', $params);
$item['mailchimp_status'] =
"<div style='width: 50%;margin: 0 auto'><img src='".$url."' style='border: none; width: 5rem; text-align: center; max-width: 100%'/></div>";
}
}

return $dataSource;
}

private function _getError($orderId, $storeId)
{
/**
Expand Down