Skip to content

Commit 4e6f6ae

Browse files
authored
Merge pull request #5249 from magento-tango/Tango-PR-01-24-2020-24
Tango PR 01-24-2020 v2.4
2 parents b64fa5b + 64cfc4e commit 4e6f6ae

File tree

5 files changed

+290
-63
lines changed

5 files changed

+290
-63
lines changed

app/code/Magento/CatalogWidget/Block/Product/ProductsList.php

Lines changed: 106 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,38 @@
66

77
namespace Magento\CatalogWidget\Block\Product;
88

9+
use Magento\Catalog\Api\CategoryRepositoryInterface;
10+
use Magento\Catalog\Block\Product\AbstractProduct;
11+
use Magento\Catalog\Block\Product\Widget\Html\Pager;
912
use Magento\Catalog\Model\Product;
10-
use Magento\Framework\App\ObjectManager;
13+
use Magento\Catalog\Model\Product\Visibility;
14+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
15+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
16+
use Magento\Catalog\Pricing\Price\FinalPrice;
17+
use Magento\CatalogWidget\Model\Rule;
1118
use Magento\Framework\App\ActionInterface;
19+
use Magento\Framework\App\Http\Context;
20+
use Magento\Framework\App\ObjectManager;
1221
use Magento\Framework\DataObject\IdentityInterface;
22+
use Magento\Framework\Exception\LocalizedException;
23+
use Magento\Framework\Exception\NoSuchEntityException;
1324
use Magento\Framework\Pricing\PriceCurrencyInterface;
1425
use Magento\Framework\Serialize\Serializer\Json;
26+
use Magento\Framework\Url\EncoderInterface;
1527
use Magento\Framework\View\LayoutFactory;
28+
use Magento\Framework\View\LayoutInterface;
29+
use Magento\Rule\Model\Condition\Combine;
30+
use Magento\Rule\Model\Condition\Sql\Builder;
1631
use Magento\Widget\Block\BlockInterface;
17-
use Magento\Framework\Url\EncoderInterface;
32+
use Magento\Widget\Helper\Conditions;
1833

1934
/**
2035
* Catalog Products List widget block
2136
*
2237
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2338
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
2439
*/
25-
class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implements BlockInterface, IdentityInterface
40+
class ProductsList extends AbstractProduct implements BlockInterface, IdentityInterface
2641
{
2742
/**
2843
* Default value for products count that will be shown
@@ -32,7 +47,7 @@ class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implem
3247
/**
3348
* Name of request parameter for page number value
3449
*
35-
* @deprecated
50+
* @deprecated @see $this->getData('page_var_name')
3651
*/
3752
const PAGE_VAR_NAME = 'np';
3853

@@ -49,41 +64,41 @@ class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implem
4964
/**
5065
* Instance of pager block
5166
*
52-
* @var \Magento\Catalog\Block\Product\Widget\Html\Pager
67+
* @var Pager
5368
*/
5469
protected $pager;
5570

5671
/**
57-
* @var \Magento\Framework\App\Http\Context
72+
* @var Context
5873
*/
5974
protected $httpContext;
6075

6176
/**
6277
* Catalog product visibility
6378
*
64-
* @var \Magento\Catalog\Model\Product\Visibility
79+
* @var Visibility
6580
*/
6681
protected $catalogProductVisibility;
6782

6883
/**
6984
* Product collection factory
7085
*
71-
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
86+
* @var CollectionFactory
7287
*/
7388
protected $productCollectionFactory;
7489

7590
/**
76-
* @var \Magento\Rule\Model\Condition\Sql\Builder
91+
* @var Builder
7792
*/
7893
protected $sqlBuilder;
7994

8095
/**
81-
* @var \Magento\CatalogWidget\Model\Rule
96+
* @var Rule
8297
*/
8398
protected $rule;
8499

85100
/**
86-
* @var \Magento\Widget\Helper\Conditions
101+
* @var Conditions
87102
*/
88103
protected $conditionsHelper;
89104

@@ -105,7 +120,7 @@ class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implem
105120
private $layoutFactory;
106121

107122
/**
108-
* @var \Magento\Framework\Url\EncoderInterface|null
123+
* @var EncoderInterface|null
109124
*/
110125
private $urlEncoder;
111126

@@ -114,29 +129,36 @@ class ProductsList extends \Magento\Catalog\Block\Product\AbstractProduct implem
114129
*/
115130
private $rendererListBlock;
116131

132+
/**
133+
* @var CategoryRepositoryInterface
134+
*/
135+
private $categoryRepository;
136+
117137
/**
118138
* @param \Magento\Catalog\Block\Product\Context $context
119-
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
120-
* @param \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility
121-
* @param \Magento\Framework\App\Http\Context $httpContext
122-
* @param \Magento\Rule\Model\Condition\Sql\Builder $sqlBuilder
123-
* @param \Magento\CatalogWidget\Model\Rule $rule
124-
* @param \Magento\Widget\Helper\Conditions $conditionsHelper
139+
* @param CollectionFactory $productCollectionFactory
140+
* @param Visibility $catalogProductVisibility
141+
* @param Context $httpContext
142+
* @param Builder $sqlBuilder
143+
* @param Rule $rule
144+
* @param Conditions $conditionsHelper
145+
* @param CategoryRepositoryInterface $categoryRepository
125146
* @param array $data
126147
* @param Json|null $json
127148
* @param LayoutFactory|null $layoutFactory
128-
* @param \Magento\Framework\Url\EncoderInterface|null $urlEncoder
149+
* @param EncoderInterface|null $urlEncoder
129150
*
130151
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
131152
*/
132153
public function __construct(
133154
\Magento\Catalog\Block\Product\Context $context,
134-
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
135-
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
136-
\Magento\Framework\App\Http\Context $httpContext,
137-
\Magento\Rule\Model\Condition\Sql\Builder $sqlBuilder,
138-
\Magento\CatalogWidget\Model\Rule $rule,
139-
\Magento\Widget\Helper\Conditions $conditionsHelper,
155+
CollectionFactory $productCollectionFactory,
156+
Visibility $catalogProductVisibility,
157+
Context $httpContext,
158+
Builder $sqlBuilder,
159+
Rule $rule,
160+
Conditions $conditionsHelper,
161+
CategoryRepositoryInterface $categoryRepository,
140162
array $data = [],
141163
Json $json = null,
142164
LayoutFactory $layoutFactory = null,
@@ -151,6 +173,7 @@ public function __construct(
151173
$this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
152174
$this->layoutFactory = $layoutFactory ?: ObjectManager::getInstance()->get(LayoutFactory::class);
153175
$this->urlEncoder = $urlEncoder ?: ObjectManager::getInstance()->get(EncoderInterface::class);
176+
$this->categoryRepository = $categoryRepository;
154177
parent::__construct(
155178
$context,
156179
$data
@@ -171,17 +194,22 @@ protected function _construct()
171194
->addColumnCountLayoutDepend('2columns-right', 4)
172195
->addColumnCountLayoutDepend('3columns', 3);
173196

174-
$this->addData([
175-
'cache_lifetime' => 86400,
176-
'cache_tags' => [\Magento\Catalog\Model\Product::CACHE_TAG,
177-
], ]);
197+
$this->addData(
198+
[
199+
'cache_lifetime' => 86400,
200+
'cache_tags' => [
201+
Product::CACHE_TAG,
202+
],
203+
]
204+
);
178205
}
179206

180207
/**
181208
* Get key pieces for caching block content
182209
*
183210
* @return array
184211
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
212+
* @throws NoSuchEntityException
185213
*/
186214
public function getCacheKeyInfo()
187215
{
@@ -195,7 +223,7 @@ public function getCacheKeyInfo()
195223
$this->_storeManager->getStore()->getId(),
196224
$this->_design->getDesignTheme()->getId(),
197225
$this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_GROUP),
198-
(int) $this->getRequest()->getParam($this->getData('page_var_name'), 1),
226+
(int)$this->getRequest()->getParam($this->getData('page_var_name'), 1),
199227
$this->getProductsPerPage(),
200228
$this->getProductsCount(),
201229
$conditions,
@@ -210,7 +238,7 @@ public function getCacheKeyInfo()
210238
* @SuppressWarnings(PHPMD.NPathComplexity)
211239
*/
212240
public function getProductPriceHtml(
213-
\Magento\Catalog\Model\Product $product,
241+
Product $product,
214242
$priceType = null,
215243
$renderZone = \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
216244
array $arguments = []
@@ -239,7 +267,7 @@ public function getProductPriceHtml(
239267
}
240268

241269
$price = $priceRender->render(
242-
\Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
270+
FinalPrice::PRICE_CODE,
243271
$product,
244272
$arguments
245273
);
@@ -253,7 +281,7 @@ public function getProductPriceHtml(
253281
protected function getDetailsRendererList()
254282
{
255283
if (empty($this->rendererListBlock)) {
256-
/** @var $layout \Magento\Framework\View\LayoutInterface */
284+
/** @var $layout LayoutInterface */
257285
$layout = $this->layoutFactory->create(['cacheable' => false]);
258286
$layout->getUpdate()->addHandle('catalog_widget_product_list')->load();
259287
$layout->generateXml();
@@ -294,12 +322,13 @@ protected function _beforeToHtml()
294322
/**
295323
* Prepare and return product collection
296324
*
297-
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
325+
* @return Collection
298326
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
327+
* @throws LocalizedException
299328
*/
300329
public function createCollection()
301330
{
302-
/** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
331+
/** @var $collection Collection */
303332
$collection = $this->productCollectionFactory->create();
304333

305334
if ($this->getData('store_id') !== null) {
@@ -327,10 +356,38 @@ public function createCollection()
327356
return $collection;
328357
}
329358

359+
/**
360+
* Update conditions if the category is an anchor category
361+
*
362+
* @param array $condition
363+
* @return array
364+
*/
365+
private function updateAnchorCategoryConditions(array $condition): array
366+
{
367+
if (array_key_exists('value', $condition)) {
368+
$categoryId = $condition['value'];
369+
370+
try {
371+
$category = $this->categoryRepository->get($categoryId, $this->_storeManager->getStore()->getId());
372+
} catch (NoSuchEntityException $e) {
373+
return $condition;
374+
}
375+
376+
$children = $category->getIsAnchor() ? $category->getChildren(true) : [];
377+
if ($children) {
378+
$children = explode(',', $children);
379+
$condition['operator'] = "()";
380+
$condition['value'] = array_merge([$categoryId], $children);
381+
}
382+
}
383+
384+
return $condition;
385+
}
386+
330387
/**
331388
* Get conditions
332389
*
333-
* @return \Magento\Rule\Model\Condition\Combine
390+
* @return Combine
334391
*/
335392
protected function getConditions()
336393
{
@@ -343,10 +400,14 @@ protected function getConditions()
343400
}
344401

345402
foreach ($conditions as $key => $condition) {
346-
if (!empty($condition['attribute'])
347-
&& in_array($condition['attribute'], ['special_from_date', 'special_to_date'])
348-
) {
349-
$conditions[$key]['value'] = date('Y-m-d H:i:s', strtotime($condition['value']));
403+
if (!empty($condition['attribute'])) {
404+
if (in_array($condition['attribute'], ['special_from_date', 'special_to_date'])) {
405+
$conditions[$key]['value'] = date('Y-m-d H:i:s', strtotime($condition['value']));
406+
}
407+
408+
if ($condition['attribute'] == 'category_ids') {
409+
$conditions[$key] = $this->updateAnchorCategoryConditions($condition);
410+
}
350411
}
351412
}
352413

@@ -412,13 +473,14 @@ protected function getPageSize()
412473
* Render pagination HTML
413474
*
414475
* @return string
476+
* @throws LocalizedException
415477
*/
416478
public function getPagerHtml()
417479
{
418480
if ($this->showPager() && $this->getProductCollection()->getSize() > $this->getProductsPerPage()) {
419481
if (!$this->pager) {
420482
$this->pager = $this->getLayout()->createBlock(
421-
\Magento\Catalog\Block\Product\Widget\Html\Pager::class,
483+
Pager::class,
422484
$this->getWidgetPagerBlockName()
423485
);
424486

@@ -448,12 +510,12 @@ public function getIdentities()
448510
if ($this->getProductCollection()) {
449511
foreach ($this->getProductCollection() as $product) {
450512
if ($product instanceof IdentityInterface) {
451-
$identities = array_merge($identities, $product->getIdentities());
513+
$identities += $product->getIdentities();
452514
}
453515
}
454516
}
455517

456-
return $identities ?: [\Magento\Catalog\Model\Product::CACHE_TAG];
518+
return $identities ?: [Product::CACHE_TAG];
457519
}
458520

459521
/**
@@ -475,7 +537,7 @@ public function getTitle()
475537
private function getPriceCurrency()
476538
{
477539
if ($this->priceCurrency === null) {
478-
$this->priceCurrency = \Magento\Framework\App\ObjectManager::getInstance()
540+
$this->priceCurrency = ObjectManager::getInstance()
479541
->get(PriceCurrencyInterface::class);
480542
}
481543
return $this->priceCurrency;

app/code/Magento/UrlRewrite/Test/Mftf/Test/AdminUrlRewritesForProductAfterImportTest.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
</annotations>
2323
<before>
2424
<comment userInput="Set the configuration for Generate category/product URL Rewrites" stepKey="commentSetURLRewriteConfiguration" />
25-
<comment userInput="Enable config to generate category/product URL Rewrites " stepKey="commentEnableConfig" />
26-
<magentoCLI command="config:set catalog/seo/generate_category_product_rewrites 1" stepKey="enableGenerateUrlRewrite"/>
2725
<createData entity="NewRootCategory" stepKey="simpleSubCategory1">
2826
<field key="parent_id">2</field>
2927
</createData>
@@ -44,8 +42,6 @@
4442
<deleteData createDataKey="simpleSubCategory3" stepKey="deleteSimpleSubCategory3"/>
4543
<deleteData createDataKey="simpleSubCategory2" stepKey="deleteSimpleSubCategory2"/>
4644
<deleteData createDataKey="simpleSubCategory1" stepKey="deleteSimpleSubCategory1"/>
47-
<comment userInput="Disable config to generate category/product URL Rewrites " stepKey="commentDisableConfig" />
48-
<magentoCLI command="config:set catalog/seo/generate_category_product_rewrites 0" stepKey="disableGenerateUrlRewrite"/>
4945
<amOnPage url="{{AdminLogoutPage.url}}" stepKey="amOnLogoutPage"/>
5046
</after>
5147

0 commit comments

Comments
 (0)