Skip to content

Commit 662bfb3

Browse files
committed
Merge remote-tracking branch 'origin/1.1-develop' into performance-MC-17868
2 parents 3766336 + fdbc6d4 commit 662bfb3

File tree

17 files changed

+962
-3
lines changed

17 files changed

+962
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilder\Model\Catalog;
10+
11+
/**
12+
* Sorting of Catalog Widget Products
13+
*/
14+
class Sorting
15+
{
16+
/**
17+
* @var array
18+
*/
19+
private $sortClasses;
20+
21+
/**
22+
* @var Sorting\Factory
23+
*/
24+
private $factory;
25+
26+
/**
27+
* @var Sorting\OptionInterface[]
28+
*/
29+
private $sortInstances = [];
30+
31+
/**
32+
* @param Sorting\Factory $factory
33+
* @param array $sortClasses
34+
* @throws \Magento\Framework\Exception\LocalizedException
35+
*/
36+
public function __construct(
37+
Sorting\Factory $factory,
38+
array $sortClasses
39+
) {
40+
$this->sortClasses = $sortClasses;
41+
$this->factory = $factory;
42+
foreach ($this->sortClasses as $key => $className) {
43+
$this->sortInstances[$key] = $this->factory->create($className);
44+
}
45+
}
46+
47+
/**
48+
* Sorting options array
49+
*
50+
* @return array
51+
*/
52+
public function getSortingOptions(): array
53+
{
54+
$options = [];
55+
foreach ($this->sortInstances as $key => $instance) {
56+
$options[$key] = $instance->getLabel();
57+
}
58+
return $options;
59+
}
60+
61+
/**
62+
* Get the instance of the first option which is None
63+
*
64+
* @param string $sortOption
65+
* @return Sorting\OptionInterface
66+
*/
67+
public function getSortingInstance($sortOption): Sorting\OptionInterface
68+
{
69+
if (isset($this->sortInstances[$sortOption])) {
70+
return $this->sortInstances[$sortOption];
71+
}
72+
}
73+
74+
/**
75+
* Applying sorting option
76+
*
77+
* @param string $option
78+
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
79+
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
80+
*/
81+
public function applySorting(
82+
string $option,
83+
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection
84+
): \Magento\Catalog\Model\ResourceModel\Product\Collection {
85+
$sortBuilder = $this->getSortingInstance($option);
86+
$_collection = $sortBuilder->sort($collection);
87+
88+
if ($_collection->isLoaded()) {
89+
$_collection->clear();
90+
}
91+
92+
return $_collection;
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilder\Model\Catalog\Sorting;
10+
11+
/**
12+
* Class Factory
13+
*/
14+
class Factory
15+
{
16+
/**
17+
* @var \Magento\Framework\ObjectManagerInterface
18+
*/
19+
private $_objectManager;
20+
21+
/**
22+
* @param \Magento\Framework\ObjectManagerInterface $objectManager
23+
*/
24+
public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
25+
{
26+
$this->_objectManager = $objectManager;
27+
}
28+
29+
/**
30+
* Create instance of sorting class
31+
*
32+
* @param string $className
33+
* @param array $data
34+
* @return OptionInterface
35+
* @throws \Magento\Framework\Exception\LocalizedException
36+
*/
37+
public function create($className, array $data = []): OptionInterface
38+
{
39+
$instance = $this->_objectManager->create($className, $data);
40+
41+
if (!$instance instanceof \Magento\PageBuilder\Model\Catalog\Sorting\OptionInterface) {
42+
throw new \Magento\Framework\Exception\LocalizedException(
43+
__('%1 doesn\'t implement OptionInterface', $className)
44+
);
45+
}
46+
return $instance;
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilder\Model\Catalog\Sorting;
10+
11+
/**
12+
* Interface OptionInterface
13+
*/
14+
interface OptionInterface
15+
{
16+
/**
17+
* Sort products in product widget collection according to specified option.
18+
*
19+
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
20+
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
21+
*/
22+
public function sort(
23+
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection
24+
): \Magento\Catalog\Model\ResourceModel\Product\Collection;
25+
26+
/**
27+
* Sorting option short description
28+
*
29+
* @return \Magento\Framework\Phrase
30+
*/
31+
public function getLabel(): \Magento\Framework\Phrase;
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilder\Model\Catalog\Sorting;
10+
11+
/**
12+
* Class Sorting Options
13+
*/
14+
class Options implements \Magento\Framework\Data\OptionSourceInterface
15+
{
16+
/**
17+
* @var \Magento\PageBuilder\Model\Catalog\Sorting
18+
*/
19+
private $sorting;
20+
21+
/**
22+
* @param \Magento\PageBuilder\Model\Catalog\Sorting $sorting
23+
*/
24+
public function __construct(
25+
\Magento\PageBuilder\Model\Catalog\Sorting $sorting
26+
) {
27+
$this->sorting = $sorting;
28+
}
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public function toOptionArray(): array
34+
{
35+
$options = [];
36+
foreach ($this->sorting->getSortingOptions() as $key => $option) {
37+
$options[] =
38+
[
39+
'value' => $key,
40+
'label' => $option
41+
];
42+
}
43+
return $options;
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilder\Model\Catalog\Sorting;
10+
11+
use Magento\Framework\DB\Select;
12+
13+
/**
14+
* Class for sorting by specified attribute
15+
*/
16+
class SimpleOption implements OptionInterface
17+
{
18+
/**
19+
* @var string
20+
*/
21+
private $label;
22+
23+
/**
24+
* @var string
25+
*/
26+
private $sortDirection;
27+
28+
/**
29+
* @var string
30+
*/
31+
private $attributeField;
32+
33+
/**
34+
* @param string $label
35+
* @param string $sortDirection
36+
* @param string $attributeField
37+
*/
38+
public function __construct(
39+
string $label,
40+
string $sortDirection,
41+
string $attributeField
42+
) {
43+
$this->label = $label;
44+
$this->sortDirection = $sortDirection;
45+
$this->attributeField = $attributeField;
46+
}
47+
48+
/**
49+
* @inheritdoc
50+
*/
51+
public function sort(
52+
\Magento\Catalog\Model\ResourceModel\Product\Collection $collection
53+
): \Magento\Catalog\Model\ResourceModel\Product\Collection {
54+
$collection->getSelect()->reset(Select::ORDER);
55+
$collection->addAttributeToSort($this->attributeField, $this->sortDirection);
56+
$collection->addAttributeToSort('entity_id', $this->sortDirection);
57+
58+
return $collection;
59+
}
60+
61+
/**
62+
* @inheritdoc
63+
*/
64+
public function getLabel(): \Magento\Framework\Phrase
65+
{
66+
return __($this->label);
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\PageBuilder\Plugin\Catalog\Block\Product;
10+
11+
use Magento\PageBuilder\Model\Catalog\Sorting;
12+
13+
/**
14+
* Catalog Products List widget block plugin
15+
*/
16+
class ProductsListPlugin
17+
{
18+
/**
19+
* @var Sorting
20+
*/
21+
private $sorting;
22+
23+
/**
24+
* ProductsListPlugin constructor.
25+
*
26+
* @param Sorting $sorting
27+
*/
28+
public function __construct(
29+
Sorting $sorting
30+
) {
31+
$this->sorting = $sorting;
32+
}
33+
34+
/**
35+
* Allow to sort product collection
36+
*
37+
* @param \Magento\CatalogWidget\Block\Product\ProductsList $subject
38+
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $result
39+
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
40+
*/
41+
public function afterCreateCollection(
42+
\Magento\CatalogWidget\Block\Product\ProductsList $subject,
43+
\Magento\Catalog\Model\ResourceModel\Product\Collection $result
44+
) {
45+
46+
$sortOption = $subject->getData('sort_order');
47+
if (isset($sortOption)) {
48+
$sortedResult = $this->sorting->applySorting($sortOption, $result);
49+
50+
return $sortedResult;
51+
} else {
52+
return $result;
53+
}
54+
}
55+
56+
/**
57+
* Include sort order in cache key
58+
*
59+
* @param \Magento\CatalogWidget\Block\Product\ProductsList $subject
60+
* @param array $cacheKeys
61+
* @return array
62+
*/
63+
public function afterGetCacheKeyInfo(\Magento\CatalogWidget\Block\Product\ProductsList $subject, array $cacheKeys)
64+
{
65+
$cacheKeys[] = $subject->getData('sort_order');
66+
return $cacheKeys;
67+
}
68+
}

app/code/Magento/PageBuilder/Test/Mftf/Data/CatalogProductData.xml

+29
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,33 @@
1111
<entity name="EditWithPageBuilderButton" type="pagebuilder_button">
1212
<data key="value">Edit with Page Builder</data>
1313
</entity>
14+
<!-- consider moving to CE -->
15+
<entity name="SortProduct1" type="product">
16+
<data key="sku" unique="suffix">sortProduct1</data>
17+
<data key="type_id">simple</data>
18+
<data key="attribute_set_id">4</data>
19+
<data key="visibility">4</data>
20+
<data key="name" unique="suffix">A Product Name</data>
21+
<data key="price">50.00</data>
22+
<data key="urlKey" unique="suffix">aproductname</data>
23+
<data key="status">1</data>
24+
<data key="quantity">5</data>
25+
<data key="weight">1</data>
26+
<requiredEntity type="product_extension_attribute">EavStockItem</requiredEntity>
27+
<requiredEntity type="custom_attribute_array">CustomAttributeCategoryIds</requiredEntity>
28+
</entity>
29+
<entity name="SortProduct2" type="product">
30+
<data key="sku" unique="suffix">sortProduct2</data>
31+
<data key="type_id">simple</data>
32+
<data key="attribute_set_id">4</data>
33+
<data key="visibility">4</data>
34+
<data key="name" unique="suffix">B Product Name</data>
35+
<data key="price">75.00</data>
36+
<data key="urlKey" unique="suffix">bproductname</data>
37+
<data key="status">1</data>
38+
<data key="quantity">15</data>
39+
<data key="weight">1</data>
40+
<requiredEntity type="product_extension_attribute">EavStockItem</requiredEntity>
41+
<requiredEntity type="custom_attribute_array">CustomAttributeCategoryIds</requiredEntity>
42+
</entity>
1443
</entities>

0 commit comments

Comments
 (0)