Skip to content

Commit b92dfb8

Browse files
🔃 [EngCom] Public Pull Requests - 2.2-develop
Accepted Public Pull Requests: - #12564: Add visibility and status filter to category product grid (by @peterjaap)
2 parents 274971a + 82f6061 commit b92dfb8

File tree

4 files changed

+163
-1
lines changed

4 files changed

+163
-1
lines changed

app/code/Magento/Catalog/Block/Adminhtml/Category/Tab/Product.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Magento\Backend\Block\Widget\Grid;
1515
use Magento\Backend\Block\Widget\Grid\Column;
1616
use Magento\Backend\Block\Widget\Grid\Extended;
17+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
18+
use Magento\Catalog\Model\Product\Visibility;
19+
use Magento\Framework\App\ObjectManager;
1720

1821
class Product extends \Magento\Backend\Block\Widget\Grid\Extended
1922
{
@@ -29,22 +32,38 @@ class Product extends \Magento\Backend\Block\Widget\Grid\Extended
2932
*/
3033
protected $_productFactory;
3134

35+
/**
36+
* @var Status
37+
*/
38+
private $status;
39+
40+
/**
41+
* @var Visibility
42+
*/
43+
private $visibility;
44+
3245
/**
3346
* @param \Magento\Backend\Block\Template\Context $context
3447
* @param \Magento\Backend\Helper\Data $backendHelper
3548
* @param \Magento\Catalog\Model\ProductFactory $productFactory
3649
* @param \Magento\Framework\Registry $coreRegistry
3750
* @param array $data
51+
* @param Visibility|null $visibility
52+
* @param Status|null $status
3853
*/
3954
public function __construct(
4055
\Magento\Backend\Block\Template\Context $context,
4156
\Magento\Backend\Helper\Data $backendHelper,
4257
\Magento\Catalog\Model\ProductFactory $productFactory,
4358
\Magento\Framework\Registry $coreRegistry,
44-
array $data = []
59+
array $data = [],
60+
Visibility $visibility = null,
61+
Status $status = null
4562
) {
4663
$this->_productFactory = $productFactory;
4764
$this->_coreRegistry = $coreRegistry;
65+
$this->visibility = $visibility ?: ObjectManager::getInstance()->get(Visibility::class);
66+
$this->status = $status ?: ObjectManager::getInstance()->get(Status::class);
4867
parent::__construct($context, $backendHelper, $data);
4968
}
5069

@@ -102,6 +121,10 @@ protected function _prepareCollection()
102121
'name'
103122
)->addAttributeToSelect(
104123
'sku'
124+
)->addAttributeToSelect(
125+
'visibility'
126+
)->addAttributeToSelect(
127+
'status'
105128
)->addAttributeToSelect(
106129
'price'
107130
)->joinField(
@@ -159,6 +182,28 @@ protected function _prepareColumns()
159182
);
160183
$this->addColumn('name', ['header' => __('Name'), 'index' => 'name']);
161184
$this->addColumn('sku', ['header' => __('SKU'), 'index' => 'sku']);
185+
$this->addColumn(
186+
'visibility',
187+
[
188+
'header' => __('Visibility'),
189+
'index' => 'visibility',
190+
'type' => 'options',
191+
'options' => $this->visibility->getOptionArray(),
192+
'header_css_class' => 'col-visibility',
193+
'column_css_class' => 'col-visibility'
194+
]
195+
);
196+
197+
$this->addColumn(
198+
'status',
199+
[
200+
'header' => __('Status'),
201+
'index' => 'status',
202+
'type' => 'options',
203+
'options' => $this->status->getOptionArray()
204+
]
205+
);
206+
162207
$this->addColumn(
163208
'price',
164209
[

dev/tests/functional/tests/app/Magento/Catalog/Test/Block/Adminhtml/Category/Edit/Section/ProductGrid.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ class ProductGrid extends Grid
2929
'name' => [
3030
'selector' => '#catalog_category_products_filter_name',
3131
],
32+
'visibility' => [
33+
'selector' => '#catalog_category_products_filter_visibility',
34+
'input' => 'select',
35+
],
36+
'status' => [
37+
'selector' => '#catalog_category_products_filter_status',
38+
'input' => 'select',
39+
],
3240
];
3341

3442
/**
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Catalog\Test\Constraint;
8+
9+
use Magento\Catalog\Test\Fixture\Category;
10+
use Magento\Catalog\Test\Page\Adminhtml\CatalogCategoryEdit;
11+
use Magento\Catalog\Test\Page\Adminhtml\CatalogCategoryIndex;
12+
use Magento\Mtf\Constraint\AbstractConstraint;
13+
14+
/**
15+
* Assert that category products grid filter works correctly.
16+
*/
17+
class AssertCategoryProductsGridFilter extends AbstractConstraint
18+
{
19+
/**
20+
* Grid columns for tests
21+
*
22+
* @var array
23+
*/
24+
private $testFilterColumns = [
25+
'visibility',
26+
];
27+
28+
/**
29+
* Assert that category products grid filter works correctly.
30+
*
31+
* @param CatalogCategoryIndex $catalogCategoryIndex
32+
* @param CatalogCategoryEdit $catalogCategoryEdit
33+
* @param Category $category
34+
* @return void
35+
*/
36+
public function processAssert(
37+
CatalogCategoryIndex $catalogCategoryIndex,
38+
CatalogCategoryEdit $catalogCategoryEdit,
39+
Category $category
40+
) {
41+
$catalogCategoryIndex->getTreeCategories()->selectCategory($category, true);
42+
$categoryProducts = $category->getDataFieldConfig('category_products')['source']->getProducts();
43+
$catalogCategoryEdit->getEditForm()->openSection('category_products');
44+
45+
foreach ($this->testFilterColumns as $field) {
46+
$this->testGridFilter($categoryProducts, $catalogCategoryEdit, $field);
47+
}
48+
}
49+
50+
/**
51+
* @param array $categoryProducts
52+
* @param CatalogCategoryEdit $catalogCategoryEdit
53+
* @param string $filterField
54+
*/
55+
private function testGridFilter(array $categoryProducts, CatalogCategoryEdit $catalogCategoryEdit, $filterField)
56+
{
57+
$productsByFilter = [];
58+
foreach ($categoryProducts as $product) {
59+
$filterValue = $product->getData($filterField);
60+
if (!isset($productsByFilter[$filterValue])) {
61+
$productsByFilter[$filterValue] = [];
62+
}
63+
$productsByFilter[$filterValue][] = $product;
64+
}
65+
66+
$productsFieldset = $catalogCategoryEdit->getEditForm()->getSection('category_products');
67+
foreach ($productsByFilter as $filterValue => $products) {
68+
$productsFieldset->getProductGrid()->search([
69+
'in_category' => 'Yes',
70+
$filterField => $filterValue,
71+
]);
72+
73+
$expectedRows = [];
74+
foreach ($products as $product) {
75+
$expectedRows[] = $product->getName();
76+
}
77+
$gridRows = $productsFieldset->getProductGrid()->getRowsData(['name']);
78+
$actualRows = array_column($gridRows, 'name');
79+
sort($expectedRows);
80+
sort($actualRows);
81+
82+
\PHPUnit_Framework_Assert::assertEquals(
83+
$expectedRows,
84+
$actualRows,
85+
"Category products grid filter '$filterField' does not work correctly"
86+
);
87+
}
88+
}
89+
90+
/**
91+
* Returns a string representation of the object.
92+
*
93+
* @return string
94+
*/
95+
public function toString()
96+
{
97+
return 'Category products grid filter works correctly';
98+
}
99+
}

dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/Category/CreateCategoryEntityTest.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,15 @@
175175
<constraint name="Magento\Catalog\Test\Constraint\AssertCategorySaveMessage" />
176176
<constraint name="Magento\Catalog\Test\Constraint\AssertCategoryOnCustomWebsite" />
177177
</variation>
178+
<variation name="CreateCategoryEntityTestVariation11_ProductsGridFilter" summary="Apply category products grid filter">
179+
<data name="addCategory" xsi:type="string">addSubcategory</data>
180+
<data name="category/data/parent_id/dataset" xsi:type="string">default_category</data>
181+
<data name="category/data/is_active" xsi:type="string">Yes</data>
182+
<data name="category/data/include_in_menu" xsi:type="string">Yes</data>
183+
<data name="category/data/name" xsi:type="string">Subcategory%isolation%</data>
184+
<data name="category/data/category_products/dataset" xsi:type="string">catalogProductSimple::default, catalogProductSimple::not_visible_individually</data>
185+
<constraint name="Magento\Catalog\Test\Constraint\AssertCategorySaveMessage" />
186+
<constraint name="Magento\Catalog\Test\Constraint\AssertCategoryProductsGridFilter" />
187+
</variation>
178188
</testCase>
179189
</config>

0 commit comments

Comments
 (0)