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

Issue1759 2.3.6 p1 #1763

Merged
merged 3 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
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
54 changes: 52 additions & 2 deletions Ui/Component/Listing/Column/Monkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class Monkey extends Column
* @var UrlInterface
*/
protected $urlBuilder;
/**
* @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
*/
private $orderCollectionFactory;

/**
* @param ContextInterface $context
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 \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory
* @param UrlInterface $urlBuilder
* @param array $components
* @param array $data
Expand All @@ -84,18 +89,19 @@ 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;
$this->_requestInterfase= $requestInterface;
$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 +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();
Expand Down Expand Up @@ -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;
}
}
55 changes: 49 additions & 6 deletions Ui/Component/Listing/Column/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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())) {
Expand Down Expand Up @@ -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')));
}
}