Skip to content

Commit 5376a81

Browse files
authored
ENGCOM-4405: Sorting by Websites not working in product grid in backoffice #20511 #20512
2 parents 03b4e15 + bbb357a commit 5376a81

File tree

2 files changed

+147
-5
lines changed

2 files changed

+147
-5
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
11+
<test name="AdminSortingByWebsitesTest">
12+
<annotations>
13+
<stories value="View sorting by websites"/>
14+
<title value="Sorting by websites in Admin"/>
15+
<description value="Sorting products by websites in Admin"/>
16+
</annotations>
17+
<before>
18+
<createData entity="_defaultCategory" stepKey="createCategory"/>
19+
<createData entity="_defaultProduct" stepKey="productAssignedToCustomWebsite">
20+
<requiredEntity createDataKey="createCategory"/>
21+
</createData>
22+
<createData entity="SimpleProduct" stepKey="productAssignedToMainWebsite">
23+
<requiredEntity createDataKey="createCategory"/>
24+
</createData>
25+
26+
<actionGroup ref="LoginAsAdmin" stepKey="loginAsAdmin"/>
27+
<!--Create new website -->
28+
<actionGroup ref="AdminCreateWebsiteActionGroup" stepKey="createAdditionalWebsite">
29+
<argument name="newWebsiteName" value="{{customWebsite.name}}"/>
30+
<argument name="websiteCode" value="{{customWebsite.code}}"/>
31+
</actionGroup>
32+
<actionGroup ref="EnableWebUrlOptions" stepKey="addStoreCodeToUrls"/>
33+
<magentoCLI command="cache:flush" stepKey="flushCacheAfterEnableWebUrlOptions"/>
34+
</before>
35+
<after>
36+
<deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
37+
<deleteData createDataKey="productAssignedToCustomWebsite" stepKey="deleteProductAssignedToCustomWebsite"/>
38+
<deleteData createDataKey="productAssignedToMainWebsite" stepKey="deleteProductAssignedToMainWebsite"/>
39+
<actionGroup ref="AdminDeleteWebsiteActionGroup" stepKey="deleteTestWebsite">
40+
<argument name="websiteName" value="{{customWebsite.name}}"/>
41+
</actionGroup>
42+
<actionGroup ref="ResetWebUrlOptions" stepKey="resetUrlOption"/>
43+
<magentoCLI command="indexer:reindex" stepKey="reindex"/>
44+
<magentoCLI command="cache:flush" stepKey="flushCache"/>
45+
<actionGroup ref="logout" stepKey="logout"/>
46+
</after>
47+
48+
<!--Assign Custom Website to Simple Product -->
49+
<amOnPage url="{{AdminProductIndexPage.url}}" stepKey="navigateToCatalogProductGrid"/>
50+
<waitForPageLoad stepKey="waitForCatalogProductGrid"/>
51+
52+
<conditionalClick selector="{{AdminProductGridFilterSection.clearFilters}}" dependentSelector="{{AdminProductGridFilterSection.clearFilters}}" visible="true" stepKey="clickClearFiltersInitial"/>
53+
<actionGroup ref="OpenEditProductOnBackendActionGroup" stepKey="assignCustomWebsiteToProduct">
54+
<argument name="product" value="$$productAssignedToCustomWebsite$$"/>
55+
</actionGroup>
56+
<scrollTo selector="{{ProductInWebsitesSection.sectionHeader}}" stepKey="scrollToWebsites"/>
57+
<conditionalClick selector="{{ProductInWebsitesSection.sectionHeader}}" dependentSelector="{{AdminProductContentSection.sectionHeaderShow}}" visible="false" stepKey="expandSection"/>
58+
<waitForPageLoad stepKey="waitForPageOpened"/>
59+
<uncheckOption selector="{{ProductInWebsitesSection.website(_defaultWebsite.name)}}" stepKey="deselectMainWebsite"/>
60+
<checkOption selector="{{ProductInWebsitesSection.website(customWebsite.name)}}" stepKey="selectWebsite"/>
61+
62+
<click selector="{{AdminProductFormActionSection.saveButton}}" stepKey="clickSave"/>
63+
<waitForLoadingMaskToDisappear stepKey="waitForProductPageToSaveAgain"/>
64+
<seeElement selector="{{AdminProductMessagesSection.successMessage}}" stepKey="seeSaveProductMessageAgain"/>
65+
66+
<!--Navigate To Product Grid To Check Website Sorting-->
67+
<amOnPage url="{{AdminProductIndexPage.url}}" stepKey="navigateToCatalogProductGridToSortByWebsite"/>
68+
<waitForPageLoad stepKey="waitForCatalogProductGridLoaded"/>
69+
70+
<!--Sorting works (By Websites) ASC-->
71+
<click selector="{{AdminProductGridSection.columnHeader('Websites')}}" stepKey="clickWebsitesHeaderToSortAsc"/>
72+
<see selector="{{AdminProductGridSection.productGridContentsOnRow('1')}}" userInput="Main Website" stepKey="checkIfProduct1WebsitesAsc"/>
73+
74+
<!--Sorting works (By Websites) DESC-->
75+
<click selector="{{AdminProductGridSection.columnHeader('Websites')}}" stepKey="clickWebsitesHeaderToSortDesc"/>
76+
<see selector="{{AdminProductGridSection.productGridContentsOnRow('1')}}" userInput="{{customWebsite.name}}" stepKey="checkIfProduct1WebsitesDesc"/>
77+
</test>
78+
</tests>

app/code/Magento/Catalog/Ui/Component/Listing/Columns/Websites.php

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
7+
declare(strict_types=1);
8+
69
namespace Magento\Catalog\Ui\Component\Listing\Columns;
710

8-
use Magento\Framework\View\Element\UiComponentFactory;
11+
use Magento\Framework\DB\Helper;
912
use Magento\Framework\View\Element\UiComponent\ContextInterface;
13+
use Magento\Framework\View\Element\UiComponentFactory;
1014
use Magento\Store\Model\StoreManagerInterface;
1115

1216
/**
17+
* Websites listing column component.
18+
*
1319
* @api
1420
* @since 100.0.2
1521
*/
@@ -20,33 +26,48 @@ class Websites extends \Magento\Ui\Component\Listing\Columns\Column
2026
*/
2127
const NAME = 'websites';
2228

29+
/**
30+
* Data for concatenated website names value.
31+
*/
32+
private $websiteNames = 'website_names';
33+
2334
/**
2435
* Store manager
2536
*
2637
* @var StoreManagerInterface
2738
*/
2839
protected $storeManager;
2940

41+
/**
42+
* @var \Magento\Framework\DB\Helper
43+
*/
44+
private $resourceHelper;
45+
3046
/**
3147
* @param ContextInterface $context
3248
* @param UiComponentFactory $uiComponentFactory
3349
* @param StoreManagerInterface $storeManager
3450
* @param array $components
3551
* @param array $data
52+
* @param Helper $resourceHelper
3653
*/
3754
public function __construct(
3855
ContextInterface $context,
3956
UiComponentFactory $uiComponentFactory,
4057
StoreManagerInterface $storeManager,
4158
array $components = [],
42-
array $data = []
59+
array $data = [],
60+
Helper $resourceHelper = null
4361
) {
4462
parent::__construct($context, $uiComponentFactory, $components, $data);
63+
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
4564
$this->storeManager = $storeManager;
65+
$this->resourceHelper = $resourceHelper ?: $objectManager->get(Helper::class);
4666
}
4767

4868
/**
49-
* {@inheritdoc}
69+
* @inheritdoc
70+
*
5071
* @deprecated 101.0.0
5172
*/
5273
public function prepareDataSource(array $dataSource)
@@ -71,9 +92,10 @@ public function prepareDataSource(array $dataSource)
7192

7293
return $dataSource;
7394
}
74-
95+
7596
/**
76-
* Prepare component configuration
97+
* Prepare component configuration.
98+
*
7799
* @return void
78100
*/
79101
public function prepare()
@@ -83,4 +105,46 @@ public function prepare()
83105
$this->_data['config']['componentDisabled'] = true;
84106
}
85107
}
108+
109+
/**
110+
* Apply sorting.
111+
*
112+
* @return void
113+
*/
114+
protected function applySorting()
115+
{
116+
$sorting = $this->getContext()->getRequestParam('sorting');
117+
$isSortable = $this->getData('config/sortable');
118+
if ($isSortable !== false
119+
&& !empty($sorting['field'])
120+
&& !empty($sorting['direction'])
121+
&& $sorting['field'] === $this->getName()
122+
) {
123+
$collection = $this->getContext()->getDataProvider()->getCollection();
124+
$collection
125+
->joinField(
126+
'websites_ids',
127+
'catalog_product_website',
128+
'website_id',
129+
'product_id=entity_id',
130+
null,
131+
'left'
132+
)
133+
->joinTable(
134+
'store_website',
135+
'website_id = websites_ids',
136+
['name'],
137+
null,
138+
'left'
139+
)
140+
->groupByAttribute('entity_id');
141+
$this->resourceHelper->addGroupConcatColumn(
142+
$collection->getSelect(),
143+
$this->websiteNames,
144+
'name'
145+
);
146+
147+
$collection->getSelect()->order($this->websiteNames . ' ' . $sorting['direction']);
148+
}
149+
}
86150
}

0 commit comments

Comments
 (0)