Skip to content

Commit

Permalink
Merge pull request #1760 from lbajsarowicz/1759-order-prefetch
Browse files Browse the repository at this point in the history
#1759 Prefetch Orders to reduce n+1 issue
  • Loading branch information
gonzaloebiz authored Aug 9, 2023
2 parents fa3e730 + 456fb13 commit b19f552
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion Ui/Component/Listing/Column/Monkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class Monkey extends Column
* @var \Magento\Sales\Model\OrderFactory
*/
protected $_orderFactory;
/**
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
*/
private $orderCollectionFactory;
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory
*/
Expand All @@ -69,6 +73,7 @@ class Monkey extends Column
* @param \Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncEcommerce\CollectionFactory $syncCommerceCF
* @param \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory $mailChimpErrorsFactory
* @param \Magento\Sales\Model\OrderFactory $orderFactory
*
* @param UrlInterface $urlBuilder
* @param array $components
* @param array $data
Expand All @@ -84,6 +89,7 @@ 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,
UrlInterface $urlBuilder,
array $components = [],
array $data = []
Expand All @@ -96,6 +102,7 @@ public function __construct(
$this->_helper = $helper;
$this->_syncCommerceCF = $syncCommerceCF;
$this->_orderFactory = $orderFactory;
$this->orderCollectionFactory = $orderCollectionFactory;
$this->_mailChimpErrorsFactory = $mailChimpErrorsFactory;
$this->urlBuilder = $urlBuilder;
parent::__construct($context, $uiComponentFactory, $components, $data);
Expand All @@ -104,13 +111,19 @@ public function __construct(
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {

$orderMap = $this->getOrderDataForIncrementIds(
$this->getOrderIncrementIds($dataSource)
);

foreach ($dataSource['data']['items'] as & $item) {
$status = $item['mailchimp_flag'];
$orderId = $item['increment_id'];
$sync = $item['mailchimp_sent'];
$error = $item['mailchimp_sync_error'];

$order = $this->_orderFactory->create()->loadByIncrementId($orderId);
$order = $orderMap[$orderId]
?? $this->_orderFactory->create()->loadByIncrementId($orderId); // Backwards Compatibility
$menu = false;
$params = ['_secure' => $this->_requestInterfase->isSecure()];
$storeId = $order->getStoreId();
Expand Down Expand Up @@ -214,4 +227,45 @@ private function _getError($orderId, $storeId)
$error = $this->_mailChimpErrorsFactory->create();
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
* @return OrderInterface[]
*/
private function getOrderDataForIncrementIds(array $incrementIds): array
{
if (empty($incrementIds)) {
return [];
}

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

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

return $ordersMap;
}
}

0 comments on commit b19f552

Please sign in to comment.