Magento 2.1 Create a filter in the product grid by new attribute
I used Magento 2.1 and want to add new filter in the product grid. But when I added my module the website loading forever.
Please help me to solve the problem. I have searched on the internet, but I didn't see any similar problem.
Bellow my code:
etc/adminhtml/di.xml
<type name="MagentoCatalogUiDataProviderProductProductDataProvider">
<arguments>
<argument name="addFieldStrategies" xsi:type="array">
<item name="configurable_options" xsi:type="object">TrainingUnit6UiDataProviderProductAddConfigurableOptionsToCollection</item>
</argument>
</arguments>
</type>
Ui/Component/Listing/Column/Options.php
namespace TrainingUnit6UiComponentListingColumn;
class Options implements MagentoFrameworkDataOptionSourceInterface
{
protected $options;
public function toOptionArray()
{
$this->options = [
[
'label' => ' ',
'value' => 0
],
[
'label' => 'One',
'value' => 1
],
[
'label' => 'Two',
'value' => 2
],
[
'label' => 'Three',
'value' => 3
],
];
return $this->options;
}
}
Ui/DataProvider/Product/AddConfigurableOptionsToCollection.php
namespace TrainingUnit6UiDataProviderProduct;
use MagentoUiDataProviderAddFilterToCollectionInterface;
use MagentoConfigurableProductModelResourceModelProductTypeConfigurableAttributeCollection as ConfigurableCollection;
use MagentoFrameworkDataCollection;
class AddConfigurableOptionsToCollection implements AddFilterToCollectionInterface {
protected $configurableOptions = null;
public function __construct(ConfigurableCollection $collection) {
$this->configurableOptions = $collection;
}
public function addFilter(Collection $collection, $field, $condition = null) {
if (isset($condition['eq']) && ($numberOfOptions = $condition['eq'])) {
$select = $this->configurableOptions->getSelect()
->reset(Zend_Db_Select::COLUMNS)
->columns(array('product_id', 'COUNT(*) as cnt'))
->group('product_id');
$res = $this->configurableOptions->getConnection()->fetchAll($select);
$ids = array();
foreach ($res as $opt) {
if ($opt['cnt'] == $numberOfOptions) {
$ids = $opt['product_id'];
}
}
$collection->addFieldToFilter('entity_id', array('in' => $ids));
}
}
}
view/adminhtml/ui_component/product_listing.xml
<container name="listing_top">
<filters name="listing_filters">
<filterSelect name="configurable_options">
<argument name="optionsProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">TrainingUnit6UiComponentListingColumnOptions</argument>
</argument>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="provider" xsi:type="string">${ $.parentName }</item>
<item name="imports" xsi:type="array">
<item name="visible" xsi:type="string">componentType = column, index = ${ $.index }:visible</item>
</item>
<item name="dataScope" xsi:type="string">configurable_options</item>
<item name="caption" xsi:type="string" translate="true">Select...</item>
<item name="label" xsi:type="string" translate="true">Configurable options</item>
</item>
</argument>
</filterSelect>
</filters>
</container>
Here is my module https://www.dropbox.com/s/lt6riovjaju4se9/unit6.zip?dl=0
magento2 magento-2.1 products filter
add a comment |
I used Magento 2.1 and want to add new filter in the product grid. But when I added my module the website loading forever.
Please help me to solve the problem. I have searched on the internet, but I didn't see any similar problem.
Bellow my code:
etc/adminhtml/di.xml
<type name="MagentoCatalogUiDataProviderProductProductDataProvider">
<arguments>
<argument name="addFieldStrategies" xsi:type="array">
<item name="configurable_options" xsi:type="object">TrainingUnit6UiDataProviderProductAddConfigurableOptionsToCollection</item>
</argument>
</arguments>
</type>
Ui/Component/Listing/Column/Options.php
namespace TrainingUnit6UiComponentListingColumn;
class Options implements MagentoFrameworkDataOptionSourceInterface
{
protected $options;
public function toOptionArray()
{
$this->options = [
[
'label' => ' ',
'value' => 0
],
[
'label' => 'One',
'value' => 1
],
[
'label' => 'Two',
'value' => 2
],
[
'label' => 'Three',
'value' => 3
],
];
return $this->options;
}
}
Ui/DataProvider/Product/AddConfigurableOptionsToCollection.php
namespace TrainingUnit6UiDataProviderProduct;
use MagentoUiDataProviderAddFilterToCollectionInterface;
use MagentoConfigurableProductModelResourceModelProductTypeConfigurableAttributeCollection as ConfigurableCollection;
use MagentoFrameworkDataCollection;
class AddConfigurableOptionsToCollection implements AddFilterToCollectionInterface {
protected $configurableOptions = null;
public function __construct(ConfigurableCollection $collection) {
$this->configurableOptions = $collection;
}
public function addFilter(Collection $collection, $field, $condition = null) {
if (isset($condition['eq']) && ($numberOfOptions = $condition['eq'])) {
$select = $this->configurableOptions->getSelect()
->reset(Zend_Db_Select::COLUMNS)
->columns(array('product_id', 'COUNT(*) as cnt'))
->group('product_id');
$res = $this->configurableOptions->getConnection()->fetchAll($select);
$ids = array();
foreach ($res as $opt) {
if ($opt['cnt'] == $numberOfOptions) {
$ids = $opt['product_id'];
}
}
$collection->addFieldToFilter('entity_id', array('in' => $ids));
}
}
}
view/adminhtml/ui_component/product_listing.xml
<container name="listing_top">
<filters name="listing_filters">
<filterSelect name="configurable_options">
<argument name="optionsProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">TrainingUnit6UiComponentListingColumnOptions</argument>
</argument>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="provider" xsi:type="string">${ $.parentName }</item>
<item name="imports" xsi:type="array">
<item name="visible" xsi:type="string">componentType = column, index = ${ $.index }:visible</item>
</item>
<item name="dataScope" xsi:type="string">configurable_options</item>
<item name="caption" xsi:type="string" translate="true">Select...</item>
<item name="label" xsi:type="string" translate="true">Configurable options</item>
</item>
</argument>
</filterSelect>
</filters>
</container>
Here is my module https://www.dropbox.com/s/lt6riovjaju4se9/unit6.zip?dl=0
magento2 magento-2.1 products filter
1
No need to add any custom code. Only setting admin side in attribute Add to Column Options : Yes & Use in Filter Options : Yes under Advanced Attribute Properties
– Abdul
May 8 '17 at 4:50
add a comment |
I used Magento 2.1 and want to add new filter in the product grid. But when I added my module the website loading forever.
Please help me to solve the problem. I have searched on the internet, but I didn't see any similar problem.
Bellow my code:
etc/adminhtml/di.xml
<type name="MagentoCatalogUiDataProviderProductProductDataProvider">
<arguments>
<argument name="addFieldStrategies" xsi:type="array">
<item name="configurable_options" xsi:type="object">TrainingUnit6UiDataProviderProductAddConfigurableOptionsToCollection</item>
</argument>
</arguments>
</type>
Ui/Component/Listing/Column/Options.php
namespace TrainingUnit6UiComponentListingColumn;
class Options implements MagentoFrameworkDataOptionSourceInterface
{
protected $options;
public function toOptionArray()
{
$this->options = [
[
'label' => ' ',
'value' => 0
],
[
'label' => 'One',
'value' => 1
],
[
'label' => 'Two',
'value' => 2
],
[
'label' => 'Three',
'value' => 3
],
];
return $this->options;
}
}
Ui/DataProvider/Product/AddConfigurableOptionsToCollection.php
namespace TrainingUnit6UiDataProviderProduct;
use MagentoUiDataProviderAddFilterToCollectionInterface;
use MagentoConfigurableProductModelResourceModelProductTypeConfigurableAttributeCollection as ConfigurableCollection;
use MagentoFrameworkDataCollection;
class AddConfigurableOptionsToCollection implements AddFilterToCollectionInterface {
protected $configurableOptions = null;
public function __construct(ConfigurableCollection $collection) {
$this->configurableOptions = $collection;
}
public function addFilter(Collection $collection, $field, $condition = null) {
if (isset($condition['eq']) && ($numberOfOptions = $condition['eq'])) {
$select = $this->configurableOptions->getSelect()
->reset(Zend_Db_Select::COLUMNS)
->columns(array('product_id', 'COUNT(*) as cnt'))
->group('product_id');
$res = $this->configurableOptions->getConnection()->fetchAll($select);
$ids = array();
foreach ($res as $opt) {
if ($opt['cnt'] == $numberOfOptions) {
$ids = $opt['product_id'];
}
}
$collection->addFieldToFilter('entity_id', array('in' => $ids));
}
}
}
view/adminhtml/ui_component/product_listing.xml
<container name="listing_top">
<filters name="listing_filters">
<filterSelect name="configurable_options">
<argument name="optionsProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">TrainingUnit6UiComponentListingColumnOptions</argument>
</argument>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="provider" xsi:type="string">${ $.parentName }</item>
<item name="imports" xsi:type="array">
<item name="visible" xsi:type="string">componentType = column, index = ${ $.index }:visible</item>
</item>
<item name="dataScope" xsi:type="string">configurable_options</item>
<item name="caption" xsi:type="string" translate="true">Select...</item>
<item name="label" xsi:type="string" translate="true">Configurable options</item>
</item>
</argument>
</filterSelect>
</filters>
</container>
Here is my module https://www.dropbox.com/s/lt6riovjaju4se9/unit6.zip?dl=0
magento2 magento-2.1 products filter
I used Magento 2.1 and want to add new filter in the product grid. But when I added my module the website loading forever.
Please help me to solve the problem. I have searched on the internet, but I didn't see any similar problem.
Bellow my code:
etc/adminhtml/di.xml
<type name="MagentoCatalogUiDataProviderProductProductDataProvider">
<arguments>
<argument name="addFieldStrategies" xsi:type="array">
<item name="configurable_options" xsi:type="object">TrainingUnit6UiDataProviderProductAddConfigurableOptionsToCollection</item>
</argument>
</arguments>
</type>
Ui/Component/Listing/Column/Options.php
namespace TrainingUnit6UiComponentListingColumn;
class Options implements MagentoFrameworkDataOptionSourceInterface
{
protected $options;
public function toOptionArray()
{
$this->options = [
[
'label' => ' ',
'value' => 0
],
[
'label' => 'One',
'value' => 1
],
[
'label' => 'Two',
'value' => 2
],
[
'label' => 'Three',
'value' => 3
],
];
return $this->options;
}
}
Ui/DataProvider/Product/AddConfigurableOptionsToCollection.php
namespace TrainingUnit6UiDataProviderProduct;
use MagentoUiDataProviderAddFilterToCollectionInterface;
use MagentoConfigurableProductModelResourceModelProductTypeConfigurableAttributeCollection as ConfigurableCollection;
use MagentoFrameworkDataCollection;
class AddConfigurableOptionsToCollection implements AddFilterToCollectionInterface {
protected $configurableOptions = null;
public function __construct(ConfigurableCollection $collection) {
$this->configurableOptions = $collection;
}
public function addFilter(Collection $collection, $field, $condition = null) {
if (isset($condition['eq']) && ($numberOfOptions = $condition['eq'])) {
$select = $this->configurableOptions->getSelect()
->reset(Zend_Db_Select::COLUMNS)
->columns(array('product_id', 'COUNT(*) as cnt'))
->group('product_id');
$res = $this->configurableOptions->getConnection()->fetchAll($select);
$ids = array();
foreach ($res as $opt) {
if ($opt['cnt'] == $numberOfOptions) {
$ids = $opt['product_id'];
}
}
$collection->addFieldToFilter('entity_id', array('in' => $ids));
}
}
}
view/adminhtml/ui_component/product_listing.xml
<container name="listing_top">
<filters name="listing_filters">
<filterSelect name="configurable_options">
<argument name="optionsProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">TrainingUnit6UiComponentListingColumnOptions</argument>
</argument>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="provider" xsi:type="string">${ $.parentName }</item>
<item name="imports" xsi:type="array">
<item name="visible" xsi:type="string">componentType = column, index = ${ $.index }:visible</item>
</item>
<item name="dataScope" xsi:type="string">configurable_options</item>
<item name="caption" xsi:type="string" translate="true">Select...</item>
<item name="label" xsi:type="string" translate="true">Configurable options</item>
</item>
</argument>
</filterSelect>
</filters>
</container>
Here is my module https://www.dropbox.com/s/lt6riovjaju4se9/unit6.zip?dl=0
magento2 magento-2.1 products filter
magento2 magento-2.1 products filter
edited May 8 '17 at 2:10
Trang Lee
asked May 5 '17 at 11:10
Trang LeeTrang Lee
7119
7119
1
No need to add any custom code. Only setting admin side in attribute Add to Column Options : Yes & Use in Filter Options : Yes under Advanced Attribute Properties
– Abdul
May 8 '17 at 4:50
add a comment |
1
No need to add any custom code. Only setting admin side in attribute Add to Column Options : Yes & Use in Filter Options : Yes under Advanced Attribute Properties
– Abdul
May 8 '17 at 4:50
1
1
No need to add any custom code. Only setting admin side in attribute Add to Column Options : Yes & Use in Filter Options : Yes under Advanced Attribute Properties
– Abdul
May 8 '17 at 4:50
No need to add any custom code. Only setting admin side in attribute Add to Column Options : Yes & Use in Filter Options : Yes under Advanced Attribute Properties
– Abdul
May 8 '17 at 4:50
add a comment |
3 Answers
3
active
oldest
votes
In the view/adminhtml/ui_component/product_listing.xml
Change <container name="listing_top">
to <listingToolbar name="listing_top">
it's worked.
add a comment |
No need to add any custom code. In magento2.x available default setting under the attribute
Go admin side Stores -> Attributes -> Product
and open your created attribute
After click Advanced Attribute Properties
tab under the Properties tab and set Yes below two fields
- Use in Filter Options: Yes (Select "Yes" to add this attribute to the list of filter options in the product grid.)
- Add to Column Options: Yes (Select "Yes" to add this attribute to the list of column options in the product grid.
)
After saving attribute and check product grid.
Please see the attached screenshot.
3
Thanks @Abdul. I know I can show that on the config product attribute. But I have many environments (dev, staging, live). I can't remember all changes when I deploy, I need change by code, but your comment is great.
– Trang Lee
May 8 '17 at 7:50
1
Sorry I can't vote your comment because my account don't enough reputation.
– Trang Lee
May 8 '17 at 8:00
add a comment |
I have found the solution. I follow by https://github.com/DRAJI/Categoryfilter. It's worked. But I don't know how to keep the filter field and remove that field in product grid?
Here is my code:
etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MagentoCatalogUiDataProviderProductProductDataProvider" type="TrainingUnit6UiDataProviderProductAddProductSeriesOptionsToCollection"/>
</config>
view/adminhtml/ui_component/product_listing.xml
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<listingToolbar name="listing_top">
</listingToolbar>
<column name="product_series" class="TrainingUnit6UiComponentListingColumnProductSeries">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="add_field" xsi:type="boolean">false</item>
<item name="dataType" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Product series</item>
<item name="sortOrder" xsi:type="number">100</item>
</item>
</argument>
</column>
</columns>
</listing>
Ui/Component/Listing/Column/ProductSeries.php
namespace TrainingUnit6UiComponentListingColumn;
use MagentoFrameworkViewElementUiComponentFactory;
use MagentoFrameworkViewElementUiComponentContextInterface;
/**
* Class Price
*/
class ProductSeries extends MagentoUiComponentListingColumnsColumn
{
protected $_productloader;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
MagentoCatalogModelProductFactory $_productloader,
array $components = ,
array $data =
) {
parent::__construct($context,$uiComponentFactory, $components, $data);
$this->_productloader = $_productloader;
}
/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
//$fieldName = $this->getData('name');
return $dataSource;
}
}
Ui/DataProvider/Product/AddProductSeriesOptionsToCollection.php
namespace TrainingUnit6UiDataProviderProduct;
class AddProductSeriesOptionsToCollection extends MagentoCatalogUiDataProviderProductProductDataProvider
{
public function addFilter(MagentoFrameworkApiFilter $filter)
{
if($filter->getField()=='unit6'){
$this->getCollection()->addCategoriesFilter(array('in' => $filter->getValue()));
}
elseif (isset($this->addFilterStrategies[$filter->getField()])) {
$this->addFilterStrategies[$filter->getField()]
->addFilter(
$this->getCollection(),
$filter->getField(),
[$filter->getConditionType() => $filter->getValue()]
);
} else {
parent::addFilter($filter);
}
}
}
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f173195%2fmagento-2-1-create-a-filter-in-the-product-grid-by-new-attribute%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In the view/adminhtml/ui_component/product_listing.xml
Change <container name="listing_top">
to <listingToolbar name="listing_top">
it's worked.
add a comment |
In the view/adminhtml/ui_component/product_listing.xml
Change <container name="listing_top">
to <listingToolbar name="listing_top">
it's worked.
add a comment |
In the view/adminhtml/ui_component/product_listing.xml
Change <container name="listing_top">
to <listingToolbar name="listing_top">
it's worked.
In the view/adminhtml/ui_component/product_listing.xml
Change <container name="listing_top">
to <listingToolbar name="listing_top">
it's worked.
answered May 8 '17 at 9:47
Trang LeeTrang Lee
7119
7119
add a comment |
add a comment |
No need to add any custom code. In magento2.x available default setting under the attribute
Go admin side Stores -> Attributes -> Product
and open your created attribute
After click Advanced Attribute Properties
tab under the Properties tab and set Yes below two fields
- Use in Filter Options: Yes (Select "Yes" to add this attribute to the list of filter options in the product grid.)
- Add to Column Options: Yes (Select "Yes" to add this attribute to the list of column options in the product grid.
)
After saving attribute and check product grid.
Please see the attached screenshot.
3
Thanks @Abdul. I know I can show that on the config product attribute. But I have many environments (dev, staging, live). I can't remember all changes when I deploy, I need change by code, but your comment is great.
– Trang Lee
May 8 '17 at 7:50
1
Sorry I can't vote your comment because my account don't enough reputation.
– Trang Lee
May 8 '17 at 8:00
add a comment |
No need to add any custom code. In magento2.x available default setting under the attribute
Go admin side Stores -> Attributes -> Product
and open your created attribute
After click Advanced Attribute Properties
tab under the Properties tab and set Yes below two fields
- Use in Filter Options: Yes (Select "Yes" to add this attribute to the list of filter options in the product grid.)
- Add to Column Options: Yes (Select "Yes" to add this attribute to the list of column options in the product grid.
)
After saving attribute and check product grid.
Please see the attached screenshot.
3
Thanks @Abdul. I know I can show that on the config product attribute. But I have many environments (dev, staging, live). I can't remember all changes when I deploy, I need change by code, but your comment is great.
– Trang Lee
May 8 '17 at 7:50
1
Sorry I can't vote your comment because my account don't enough reputation.
– Trang Lee
May 8 '17 at 8:00
add a comment |
No need to add any custom code. In magento2.x available default setting under the attribute
Go admin side Stores -> Attributes -> Product
and open your created attribute
After click Advanced Attribute Properties
tab under the Properties tab and set Yes below two fields
- Use in Filter Options: Yes (Select "Yes" to add this attribute to the list of filter options in the product grid.)
- Add to Column Options: Yes (Select "Yes" to add this attribute to the list of column options in the product grid.
)
After saving attribute and check product grid.
Please see the attached screenshot.
No need to add any custom code. In magento2.x available default setting under the attribute
Go admin side Stores -> Attributes -> Product
and open your created attribute
After click Advanced Attribute Properties
tab under the Properties tab and set Yes below two fields
- Use in Filter Options: Yes (Select "Yes" to add this attribute to the list of filter options in the product grid.)
- Add to Column Options: Yes (Select "Yes" to add this attribute to the list of column options in the product grid.
)
After saving attribute and check product grid.
Please see the attached screenshot.
edited 9 mins ago
Utsav Gupta
15012
15012
answered May 8 '17 at 5:10
AbdulAbdul
7,99511135
7,99511135
3
Thanks @Abdul. I know I can show that on the config product attribute. But I have many environments (dev, staging, live). I can't remember all changes when I deploy, I need change by code, but your comment is great.
– Trang Lee
May 8 '17 at 7:50
1
Sorry I can't vote your comment because my account don't enough reputation.
– Trang Lee
May 8 '17 at 8:00
add a comment |
3
Thanks @Abdul. I know I can show that on the config product attribute. But I have many environments (dev, staging, live). I can't remember all changes when I deploy, I need change by code, but your comment is great.
– Trang Lee
May 8 '17 at 7:50
1
Sorry I can't vote your comment because my account don't enough reputation.
– Trang Lee
May 8 '17 at 8:00
3
3
Thanks @Abdul. I know I can show that on the config product attribute. But I have many environments (dev, staging, live). I can't remember all changes when I deploy, I need change by code, but your comment is great.
– Trang Lee
May 8 '17 at 7:50
Thanks @Abdul. I know I can show that on the config product attribute. But I have many environments (dev, staging, live). I can't remember all changes when I deploy, I need change by code, but your comment is great.
– Trang Lee
May 8 '17 at 7:50
1
1
Sorry I can't vote your comment because my account don't enough reputation.
– Trang Lee
May 8 '17 at 8:00
Sorry I can't vote your comment because my account don't enough reputation.
– Trang Lee
May 8 '17 at 8:00
add a comment |
I have found the solution. I follow by https://github.com/DRAJI/Categoryfilter. It's worked. But I don't know how to keep the filter field and remove that field in product grid?
Here is my code:
etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MagentoCatalogUiDataProviderProductProductDataProvider" type="TrainingUnit6UiDataProviderProductAddProductSeriesOptionsToCollection"/>
</config>
view/adminhtml/ui_component/product_listing.xml
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<listingToolbar name="listing_top">
</listingToolbar>
<column name="product_series" class="TrainingUnit6UiComponentListingColumnProductSeries">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="add_field" xsi:type="boolean">false</item>
<item name="dataType" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Product series</item>
<item name="sortOrder" xsi:type="number">100</item>
</item>
</argument>
</column>
</columns>
</listing>
Ui/Component/Listing/Column/ProductSeries.php
namespace TrainingUnit6UiComponentListingColumn;
use MagentoFrameworkViewElementUiComponentFactory;
use MagentoFrameworkViewElementUiComponentContextInterface;
/**
* Class Price
*/
class ProductSeries extends MagentoUiComponentListingColumnsColumn
{
protected $_productloader;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
MagentoCatalogModelProductFactory $_productloader,
array $components = ,
array $data =
) {
parent::__construct($context,$uiComponentFactory, $components, $data);
$this->_productloader = $_productloader;
}
/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
//$fieldName = $this->getData('name');
return $dataSource;
}
}
Ui/DataProvider/Product/AddProductSeriesOptionsToCollection.php
namespace TrainingUnit6UiDataProviderProduct;
class AddProductSeriesOptionsToCollection extends MagentoCatalogUiDataProviderProductProductDataProvider
{
public function addFilter(MagentoFrameworkApiFilter $filter)
{
if($filter->getField()=='unit6'){
$this->getCollection()->addCategoriesFilter(array('in' => $filter->getValue()));
}
elseif (isset($this->addFilterStrategies[$filter->getField()])) {
$this->addFilterStrategies[$filter->getField()]
->addFilter(
$this->getCollection(),
$filter->getField(),
[$filter->getConditionType() => $filter->getValue()]
);
} else {
parent::addFilter($filter);
}
}
}
add a comment |
I have found the solution. I follow by https://github.com/DRAJI/Categoryfilter. It's worked. But I don't know how to keep the filter field and remove that field in product grid?
Here is my code:
etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MagentoCatalogUiDataProviderProductProductDataProvider" type="TrainingUnit6UiDataProviderProductAddProductSeriesOptionsToCollection"/>
</config>
view/adminhtml/ui_component/product_listing.xml
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<listingToolbar name="listing_top">
</listingToolbar>
<column name="product_series" class="TrainingUnit6UiComponentListingColumnProductSeries">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="add_field" xsi:type="boolean">false</item>
<item name="dataType" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Product series</item>
<item name="sortOrder" xsi:type="number">100</item>
</item>
</argument>
</column>
</columns>
</listing>
Ui/Component/Listing/Column/ProductSeries.php
namespace TrainingUnit6UiComponentListingColumn;
use MagentoFrameworkViewElementUiComponentFactory;
use MagentoFrameworkViewElementUiComponentContextInterface;
/**
* Class Price
*/
class ProductSeries extends MagentoUiComponentListingColumnsColumn
{
protected $_productloader;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
MagentoCatalogModelProductFactory $_productloader,
array $components = ,
array $data =
) {
parent::__construct($context,$uiComponentFactory, $components, $data);
$this->_productloader = $_productloader;
}
/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
//$fieldName = $this->getData('name');
return $dataSource;
}
}
Ui/DataProvider/Product/AddProductSeriesOptionsToCollection.php
namespace TrainingUnit6UiDataProviderProduct;
class AddProductSeriesOptionsToCollection extends MagentoCatalogUiDataProviderProductProductDataProvider
{
public function addFilter(MagentoFrameworkApiFilter $filter)
{
if($filter->getField()=='unit6'){
$this->getCollection()->addCategoriesFilter(array('in' => $filter->getValue()));
}
elseif (isset($this->addFilterStrategies[$filter->getField()])) {
$this->addFilterStrategies[$filter->getField()]
->addFilter(
$this->getCollection(),
$filter->getField(),
[$filter->getConditionType() => $filter->getValue()]
);
} else {
parent::addFilter($filter);
}
}
}
add a comment |
I have found the solution. I follow by https://github.com/DRAJI/Categoryfilter. It's worked. But I don't know how to keep the filter field and remove that field in product grid?
Here is my code:
etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MagentoCatalogUiDataProviderProductProductDataProvider" type="TrainingUnit6UiDataProviderProductAddProductSeriesOptionsToCollection"/>
</config>
view/adminhtml/ui_component/product_listing.xml
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<listingToolbar name="listing_top">
</listingToolbar>
<column name="product_series" class="TrainingUnit6UiComponentListingColumnProductSeries">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="add_field" xsi:type="boolean">false</item>
<item name="dataType" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Product series</item>
<item name="sortOrder" xsi:type="number">100</item>
</item>
</argument>
</column>
</columns>
</listing>
Ui/Component/Listing/Column/ProductSeries.php
namespace TrainingUnit6UiComponentListingColumn;
use MagentoFrameworkViewElementUiComponentFactory;
use MagentoFrameworkViewElementUiComponentContextInterface;
/**
* Class Price
*/
class ProductSeries extends MagentoUiComponentListingColumnsColumn
{
protected $_productloader;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
MagentoCatalogModelProductFactory $_productloader,
array $components = ,
array $data =
) {
parent::__construct($context,$uiComponentFactory, $components, $data);
$this->_productloader = $_productloader;
}
/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
//$fieldName = $this->getData('name');
return $dataSource;
}
}
Ui/DataProvider/Product/AddProductSeriesOptionsToCollection.php
namespace TrainingUnit6UiDataProviderProduct;
class AddProductSeriesOptionsToCollection extends MagentoCatalogUiDataProviderProductProductDataProvider
{
public function addFilter(MagentoFrameworkApiFilter $filter)
{
if($filter->getField()=='unit6'){
$this->getCollection()->addCategoriesFilter(array('in' => $filter->getValue()));
}
elseif (isset($this->addFilterStrategies[$filter->getField()])) {
$this->addFilterStrategies[$filter->getField()]
->addFilter(
$this->getCollection(),
$filter->getField(),
[$filter->getConditionType() => $filter->getValue()]
);
} else {
parent::addFilter($filter);
}
}
}
I have found the solution. I follow by https://github.com/DRAJI/Categoryfilter. It's worked. But I don't know how to keep the filter field and remove that field in product grid?
Here is my code:
etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MagentoCatalogUiDataProviderProductProductDataProvider" type="TrainingUnit6UiDataProviderProductAddProductSeriesOptionsToCollection"/>
</config>
view/adminhtml/ui_component/product_listing.xml
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<listingToolbar name="listing_top">
</listingToolbar>
<column name="product_series" class="TrainingUnit6UiComponentListingColumnProductSeries">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="add_field" xsi:type="boolean">false</item>
<item name="dataType" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Product series</item>
<item name="sortOrder" xsi:type="number">100</item>
</item>
</argument>
</column>
</columns>
</listing>
Ui/Component/Listing/Column/ProductSeries.php
namespace TrainingUnit6UiComponentListingColumn;
use MagentoFrameworkViewElementUiComponentFactory;
use MagentoFrameworkViewElementUiComponentContextInterface;
/**
* Class Price
*/
class ProductSeries extends MagentoUiComponentListingColumnsColumn
{
protected $_productloader;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
MagentoCatalogModelProductFactory $_productloader,
array $components = ,
array $data =
) {
parent::__construct($context,$uiComponentFactory, $components, $data);
$this->_productloader = $_productloader;
}
/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
//$fieldName = $this->getData('name');
return $dataSource;
}
}
Ui/DataProvider/Product/AddProductSeriesOptionsToCollection.php
namespace TrainingUnit6UiDataProviderProduct;
class AddProductSeriesOptionsToCollection extends MagentoCatalogUiDataProviderProductProductDataProvider
{
public function addFilter(MagentoFrameworkApiFilter $filter)
{
if($filter->getField()=='unit6'){
$this->getCollection()->addCategoriesFilter(array('in' => $filter->getValue()));
}
elseif (isset($this->addFilterStrategies[$filter->getField()])) {
$this->addFilterStrategies[$filter->getField()]
->addFilter(
$this->getCollection(),
$filter->getField(),
[$filter->getConditionType() => $filter->getValue()]
);
} else {
parent::addFilter($filter);
}
}
}
edited May 8 '17 at 8:25
answered May 8 '17 at 8:14
Trang LeeTrang Lee
7119
7119
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f173195%2fmagento-2-1-create-a-filter-in-the-product-grid-by-new-attribute%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
No need to add any custom code. Only setting admin side in attribute Add to Column Options : Yes & Use in Filter Options : Yes under Advanced Attribute Properties
– Abdul
May 8 '17 at 4:50