Skip to content

Commit f8c7333

Browse files
authored
Merge pull request #5248 from magento-tsg-csl3/2.4-develop-pr10
[TSG-CSL3] For 2.4 (pr10)
2 parents 0ac3443 + c11cfd0 commit f8c7333

File tree

28 files changed

+928
-160
lines changed

28 files changed

+928
-160
lines changed

app/code/Magento/Catalog/Controller/Product/Compare/Index.php

+2-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Magento\Framework\View\Result\PageFactory;
1313

1414
/**
15+
* View products compare in frontend
16+
*
1517
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1618
*/
1719
class Index extends \Magento\Catalog\Controller\Product\Compare implements HttpGetActionInterface
@@ -74,23 +76,12 @@ public function __construct(
7476
*/
7577
public function execute()
7678
{
77-
$items = $this->getRequest()->getParam('items');
78-
7979
$beforeUrl = $this->getRequest()->getParam(self::PARAM_NAME_URL_ENCODED);
8080
if ($beforeUrl) {
8181
$this->_catalogSession->setBeforeCompareUrl(
8282
$this->urlDecoder->decode($beforeUrl)
8383
);
8484
}
85-
86-
if ($items) {
87-
$items = explode(',', $items);
88-
/** @var \Magento\Catalog\Model\Product\Compare\ListCompare $list */
89-
$list = $this->_catalogProductCompareList;
90-
$list->addProducts($items);
91-
$resultRedirect = $this->resultRedirectFactory->create();
92-
return $resultRedirect->setPath('*/*/*');
93-
}
9485
return $this->resultPageFactory->create();
9586
}
9687
}

app/code/Magento/Catalog/Helper/Product/Compare.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* @api
1515
* @SuppressWarnings(PHPMD.LongVariable)
1616
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1718
* @since 100.0.2
1819
*/
1920
class Compare extends \Magento\Framework\Url\Helper\Data
@@ -145,16 +146,9 @@ public function __construct(
145146
*/
146147
public function getListUrl()
147148
{
148-
$itemIds = [];
149-
foreach ($this->getItemCollection() as $item) {
150-
$itemIds[] = $item->getId();
151-
}
152-
153149
$params = [
154-
'items' => implode(',', $itemIds),
155150
\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
156151
];
157-
158152
return $this->_getUrl('catalog/product_compare', $params);
159153
}
160154

app/code/Magento/Catalog/Test/Unit/Controller/Product/Compare/IndexTest.php

-28
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ public function testExecute()
129129
->method('getParam')
130130
->willReturnMap(
131131
[
132-
['items', null, null],
133132
['uenc', null, $beforeUrl],
134133
]
135134
);
@@ -141,34 +140,7 @@ public function testExecute()
141140
->method('setBeforeCompareUrl')
142141
->with($beforeUrl . '1')
143142
->willReturnSelf();
144-
$this->listCompareMock->expects($this->never())->method('addProducts');
145143
$this->redirectFactoryMock->expects($this->never())->method('create');
146144
$this->index->execute();
147145
}
148-
149-
public function testExecuteWithItems()
150-
{
151-
$this->request->expects($this->any())
152-
->method('getParam')
153-
->willReturnMap(
154-
[
155-
['items', null, '1,2,3'],
156-
['uenc', null, null],
157-
]
158-
);
159-
$this->decoderMock->expects($this->never())->method('decode');
160-
$this->catalogSession->expects($this->never())->method('setBeforeCompareUrl');
161-
162-
$this->listCompareMock->expects($this->once())
163-
->method('addProducts')
164-
->with([1, 2, 3]);
165-
$redirect = $this->createPartialMock(\Magento\Framework\Controller\Result\Redirect::class, ['setPath']);
166-
$redirect->expects($this->once())
167-
->method('setPath')
168-
->with('*/*/*');
169-
$this->redirectFactoryMock->expects($this->once())
170-
->method('create')
171-
->willReturn($redirect);
172-
$this->index->execute();
173-
}
174146
}

app/code/Magento/CatalogImportExport/Model/Export/Product.php

+17-11
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
*/
66
namespace Magento\CatalogImportExport\Model\Export;
77

8+
use Magento\Catalog\Model\Product as ProductEntity;
89
use Magento\Catalog\Model\ResourceModel\Product\Option\Collection;
10+
use Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
911
use Magento\CatalogImportExport\Model\Import\Product\CategoryProcessor;
12+
use Magento\Framework\App\ObjectManager;
1013
use Magento\ImportExport\Model\Import;
11-
use \Magento\Store\Model\Store;
12-
use \Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
13-
use Magento\Catalog\Model\Product as ProductEntity;
14+
use Magento\Store\Model\Store;
1415

1516
/**
1617
* Export entity product model
@@ -21,6 +22,8 @@
2122
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
2223
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2324
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
25+
* @SuppressWarnings(PHPMD.ExcessiveClassLength)
26+
* @SuppressWarnings(PHPMD.TooManyMethods)
2427
* @since 100.0.2
2528
*/
2629
class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
@@ -348,6 +351,10 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
348351
* @var string
349352
*/
350353
private $productEntityLinkField;
354+
/**
355+
* @var ProductFilterInterface
356+
*/
357+
private $filter;
351358

352359
/**
353360
* Product constructor.
@@ -369,6 +376,7 @@ class Product extends \Magento\ImportExport\Model\Export\Entity\AbstractEntity
369376
* @param ProductEntity\LinkTypeProvider $linkTypeProvider
370377
* @param RowCustomizerInterface $rowCustomizer
371378
* @param array $dateAttrCodes
379+
* @param ProductFilterInterface $filter
372380
* @throws \Magento\Framework\Exception\LocalizedException
373381
*/
374382
public function __construct(
@@ -388,7 +396,8 @@ public function __construct(
388396
\Magento\CatalogImportExport\Model\Export\Product\Type\Factory $_typeFactory,
389397
\Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider,
390398
\Magento\CatalogImportExport\Model\Export\RowCustomizerInterface $rowCustomizer,
391-
array $dateAttrCodes = []
399+
array $dateAttrCodes = [],
400+
?ProductFilterInterface $filter = null
392401
) {
393402
$this->_entityCollectionFactory = $collectionFactory;
394403
$this->_exportConfig = $exportConfig;
@@ -404,6 +413,7 @@ public function __construct(
404413
$this->_linkTypeProvider = $linkTypeProvider;
405414
$this->rowCustomizer = $rowCustomizer;
406415
$this->dateAttrCodes = array_merge($this->dateAttrCodes, $dateAttrCodes);
416+
$this->filter = $filter ?? ObjectManager::getInstance()->get(ProductFilterInterface::class);
407417

408418
parent::__construct($localeDate, $config, $resource, $storeManager);
409419

@@ -819,9 +829,11 @@ protected function getItemsPerPage()
819829
case 'g':
820830
$memoryLimit *= 1024;
821831
// fall-through intentional
832+
// no break
822833
case 'm':
823834
$memoryLimit *= 1024;
824835
// fall-through intentional
836+
// no break
825837
case 'k':
826838
$memoryLimit *= 1024;
827839
break;
@@ -913,12 +925,7 @@ protected function _prepareEntityCollection(\Magento\Eav\Model\Entity\Collection
913925
$exportFilter = !empty($this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP]) ?
914926
$this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP] : [];
915927

916-
if (isset($exportFilter['category_ids'])
917-
&& trim($exportFilter['category_ids'])
918-
&& $collection instanceof \Magento\Catalog\Model\ResourceModel\Product\Collection
919-
) {
920-
$collection->addCategoriesFilter(['in' => explode(',', $exportFilter['category_ids'])]);
921-
}
928+
$collection = $this->filter->filter($collection, $exportFilter);
922929

923930
return parent::_prepareEntityCollection($collection);
924931
}
@@ -979,7 +986,6 @@ protected function loadCollection(): array
979986
$collection = $this->_getEntityCollection();
980987
foreach (array_keys($this->_storeIdToCode) as $storeId) {
981988
$collection->setOrder('entity_id', 'asc');
982-
$this->_prepareEntityCollection($collection);
983989
$collection->setStoreId($storeId);
984990
$collection->load();
985991
foreach ($collection as $itemId => $item) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogImportExport\Model\Export\Product;
9+
10+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
11+
use Magento\CatalogImportExport\Model\Export\ProductFilterInterface;
12+
13+
/**
14+
* Category filter for products export
15+
*/
16+
class CategoryFilter implements ProductFilterInterface
17+
{
18+
private const NAME = 'category_ids';
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
public function filter(Collection $collection, array $filters): Collection
24+
{
25+
$value = trim($filters[self::NAME] ?? '');
26+
if ($value) {
27+
$collection->addCategoriesFilter(['in' => explode(',', $value)]);
28+
}
29+
return $collection;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogImportExport\Model\Export\Product;
9+
10+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
11+
use Magento\CatalogInventory\Model\Configuration;
12+
use Magento\CatalogInventory\Model\ResourceModel\Stock\Item as StockItemResourceModel;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Store\Model\ScopeInterface;
15+
16+
/**
17+
* Stock status collection filter
18+
*/
19+
class Stock
20+
{
21+
/**
22+
* @var ScopeConfigInterface
23+
*/
24+
private $scopeConfig;
25+
/**
26+
* @var StockItemResourceModel
27+
*/
28+
private $stockItemResourceModel;
29+
30+
/**
31+
* @param ScopeConfigInterface $scopeConfig
32+
* @param StockItemResourceModel $stockItemResourceModel
33+
*/
34+
public function __construct(
35+
ScopeConfigInterface $scopeConfig,
36+
StockItemResourceModel $stockItemResourceModel
37+
) {
38+
$this->scopeConfig = $scopeConfig;
39+
$this->stockItemResourceModel = $stockItemResourceModel;
40+
}
41+
42+
/**
43+
* Filter provided collection to return only "in stock" products
44+
*
45+
* @param Collection $collection
46+
* @return Collection
47+
*/
48+
public function addInStockFilterToCollection(Collection $collection): Collection
49+
{
50+
$manageStock = $this->scopeConfig->getValue(
51+
Configuration::XML_PATH_MANAGE_STOCK,
52+
ScopeInterface::SCOPE_STORE
53+
);
54+
$cond = [
55+
'{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=1 AND {{table}}.is_in_stock=1',
56+
'{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=0'
57+
];
58+
59+
if ($manageStock) {
60+
$cond[] = '{{table}}.use_config_manage_stock = 1 AND {{table}}.is_in_stock=1';
61+
} else {
62+
$cond[] = '{{table}}.use_config_manage_stock = 1';
63+
}
64+
return $this->addFilterToCollection($collection, '(' . join(') OR (', $cond) . ')');
65+
}
66+
67+
/**
68+
* Filter provided collection to return only "out of stock" products
69+
*
70+
* @param Collection $collection
71+
* @return Collection
72+
*/
73+
public function addOutOfStockFilterToCollection(Collection $collection): Collection
74+
{
75+
$manageStock = $this->scopeConfig->getValue(
76+
Configuration::XML_PATH_MANAGE_STOCK,
77+
ScopeInterface::SCOPE_STORE
78+
);
79+
$cond = [
80+
'{{table}}.use_config_manage_stock = 0 AND {{table}}.manage_stock=1 AND {{table}}.is_in_stock=0',
81+
];
82+
83+
if ($manageStock) {
84+
$cond[] = '{{table}}.use_config_manage_stock = 1 AND {{table}}.is_in_stock=0';
85+
}
86+
return $this->addFilterToCollection($collection, '(' . join(') OR (', $cond) . ')');
87+
}
88+
89+
/**
90+
* Add stock status filter to the collection
91+
*
92+
* @param Collection $collection
93+
* @param string $condition
94+
* @return Collection
95+
*/
96+
private function addFilterToCollection(Collection $collection, string $condition): Collection
97+
{
98+
$condition = str_replace(
99+
'{{table}}',
100+
'inventory_stock_item_filter',
101+
'({{table}}.product_id=e.entity_id) AND (' . $condition . ')'
102+
);
103+
$collection->getSelect()
104+
->joinInner(
105+
['inventory_stock_item_filter' => $this->stockItemResourceModel->getMainTable()],
106+
$condition,
107+
[]
108+
);
109+
return $collection;
110+
}
111+
}

0 commit comments

Comments
 (0)