Skip to content

Commit

Permalink
Merge pull request #1765 from mailchimp/Issue1759-2.2
Browse files Browse the repository at this point in the history
 closes #1759 for magento 2.2
  • Loading branch information
gonzaloebiz authored Aug 10, 2023
2 parents c445ba9 + df1e129 commit 7cd935e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 31 deletions.
74 changes: 49 additions & 25 deletions Ui/Component/Listing/Column/Monkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\UrlInterface;
use Ebizmarts\MailChimp\Helper\Sync as SyncHelper;


class Monkey extends Column
{
Expand All @@ -42,10 +42,6 @@ class Monkey extends Column
* @var \Ebizmarts\MailChimp\Helper\Data
*/
protected $_helper;
/**
* @var SyncHelper
*/
private $syncHelper;
/**
* @var \Ebizmarts\MailChimp\Model\ResourceModel\MailChimpSyncEcommerce\CollectionFactory
*/
Expand All @@ -62,23 +58,8 @@ class Monkey extends Column
* @var UrlInterface
*/
protected $urlBuilder;
private $orderCollectionFactory;

/**
* @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 \Ebizmarts\MailChimp\Helper\Data $helper
* @param SyncHelper $syncHelper
* @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
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
Expand All @@ -87,23 +68,22 @@ public function __construct(
\Magento\Framework\App\RequestInterface $requestInterface,
SearchCriteriaBuilder $criteria,
\Ebizmarts\MailChimp\Helper\Data $helper,
SyncHelper $syncHelper,
\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;
$this->_requestInterfase= $requestInterface;
$this->_helper = $helper;
$this->syncHelper = $syncHelper;
$this->_syncCommerceCF = $syncCommerceCF;
$this->_orderFactory = $orderFactory;
$this->orderCollectionFactory = $orderCollectionFactory;
$this->_mailChimpErrorsFactory = $mailChimpErrorsFactory;
$this->urlBuilder = $urlBuilder;
parent::__construct($context, $uiComponentFactory, $components, $data);
Expand All @@ -112,13 +92,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($item['increment_id']);
$order = $orderMap[$orderId]
?? $this->_orderFactory->create()->loadByIncrementId($orderId); // Backwards Compatibility
$menu = false;
$params = ['_secure' => $this->_requestInterfase->isSecure()];
$storeId = $order->getStoreId();
Expand Down Expand Up @@ -222,4 +206,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;
}
}
56 changes: 50 additions & 6 deletions Ui/Component/Listing/Column/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +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;
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
*/
Expand All @@ -29,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
Expand All @@ -45,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,
Expand All @@ -57,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())) {
Expand Down Expand Up @@ -157,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')));
}
}

0 comments on commit 7cd935e

Please sign in to comment.