diff --git a/Ui/Component/Listing/Column/Monkey.php b/Ui/Component/Listing/Column/Monkey.php index f9803011..941903e9 100644 --- a/Ui/Component/Listing/Column/Monkey.php +++ b/Ui/Component/Listing/Column/Monkey.php @@ -57,6 +57,10 @@ class Monkey extends Column * @var UrlInterface */ protected $urlBuilder; + /** + * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory + */ + private $orderCollectionFactory; /** * @param ContextInterface $context @@ -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 \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory * @param UrlInterface $urlBuilder * @param array $components * @param array $data @@ -84,11 +89,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, UrlInterface $urlBuilder, array $components = [], array $data = [] ) { - $this->_orderRepository = $orderRepository; $this->_searchCriteria = $criteria; $this->_assetRepository = $assetRepository; @@ -96,6 +101,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); @@ -104,13 +110,17 @@ 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(); @@ -214,4 +224,44 @@ 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; + } } diff --git a/Ui/Component/Listing/Column/Products.php b/Ui/Component/Listing/Column/Products.php index 87500de4..23001fff 100644 --- a/Ui/Component/Listing/Column/Products.php +++ b/Ui/Component/Listing/Column/Products.php @@ -2,14 +2,23 @@ namespace Ebizmarts\MailChimp\Ui\Component\Listing\Column; use Magento\Catalog\Model\ProductFactory; +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory; +use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ProductTypeConfigurable; +use Magento\Catalog\Model\Product\Type as ProductType; +use Magento\Downloadable\Model\Product\Type as ProductTypeDownloadable; use Magento\Framework\App\RequestInterface; use Magento\Framework\View\Element\UiComponent\ContextInterface; use Magento\Framework\View\Element\UiComponentFactory; use Magento\Ui\Component\Listing\Columns\Column; - class Products extends Column { + private const SUPPORTED_PRODUCT_TYPES = [ + ProductType::TYPE_SIMPLE, + ProductType::TYPE_VIRTUAL, + ProductTypeConfigurable::TYPE_CODE, + ProductTypeDownloadable::TYPE_DOWNLOADABLE + ]; /** * @var ProductFactory */ @@ -30,11 +39,16 @@ class Products extends Column * @var \Ebizmarts\MailChimp\Model\MailChimpErrorsFactory */ protected $_mailChimpErrorsFactory; + /** + * @var ProductCollectionFactory + */ + private $productCollectionFactory; /** * @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 @@ -46,6 +60,7 @@ public function __construct( ContextInterface $context, UiComponentFactory $uiComponentFactory, ProductFactory $productFactory, + ProductCollectionFactory $productCollectionFactory, RequestInterface $requestInterface, \Ebizmarts\MailChimp\Helper\Data $helper, \Magento\Framework\View\Asset\Repository $assetRepository, @@ -58,25 +73,27 @@ public function __construct( $this->_helper = $helper; $this->_assetRepository = $assetRepository; $this->_mailChimpErrorsFactory = $mailChimpErrorsFactory; + $this->productCollectionFactory = $productCollectionFactory; parent::__construct($context, $uiComponentFactory, $components, $data); } public function prepareDataSource(array $dataSource) { if (isset($dataSource['data']['items'])) { + $productsMap = $this->getProductsByEntityIds( + $this->getProductsIds($dataSource) + ); foreach ($dataSource['data']['items'] as & $item) { /** * @var $product \Magento\Catalog\Model\Product */ - $product = $this->_productFactory->create()->load($item['entity_id']); + $product = $productsMap[$item['entity_id']] + ?? $this->_productFactory->create()->load($item['entity_id']); // Backwards Compatibility $params = ['_secure' => $this->_requestInterface->isSecure()]; $alt = ''; $url = ''; $text = ''; - if ($product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE || - $product->getTypeId() == \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL || - $product->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE || - $product->getTypeId() == \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { + if (in_array($product->getTypeId(), self::SUPPORTED_PRODUCT_TYPES, true)) { $url = ''; $text = ''; if ($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_ACTIVE, $product->getStoreId())) { @@ -158,4 +175,30 @@ private function _getError($productId, $storeId) $error = $this->_mailChimpErrorsFactory->create(); return $error->getByStoreIdType($storeId, $productId, \Ebizmarts\MailChimp\Helper\Data::IS_PRODUCT); } + private function getProductsByEntityIds(array $productIds): array + { + if (empty($productIds)) { + return []; + } + + /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productsCollection */ + $productsCollection = $this->productCollectionFactory->create(); + $productsCollection->addAttributeToFilter('entity_id', ['in' => $productIds]); + + $productsMap = []; + foreach ($productsCollection->getItems() as $product) { + $productsMap[$product->getId()] = $product; + } + + return $productsMap; + } + + private function getProductsIds(array $dataSource) + { + if (!isset($dataSource['data']['items'])) { + return []; + } + + return array_filter(array_unique(array_column($dataSource['data']['items'], 'entity_id'))); + } }