Magento 2: Get list of products on basis of attribute codeGet color values used in productsCode for filtering products by attributes?Get Products list with all product attributes in MagentoCan't get custom attribute from getUsedProductsCreate Dropdown Attribute for ProductsHow to list attributes for all SKUs in the system? My code only shows half productsGet product attribute on product list in one queryList products by dropdown attributeMagento 2.2 - How can I pass the color attribute from the category list to the product detail page?Get list of all modules using code in Magento 2
What is the meaning of "prairie-dog" in this sentence?
How do resistors generate different heat if we make the current fixed and changed the voltage and resistance? Notice the flow of charge is constant
Can one block with a protection from color creature?
Category-theoretic treatment of diffs, patches and merging?
Is there an In-Universe reason why Thor and the Asgardians think Rocket is a rabbit?
Writing an ace/aro character?
Why did Robert F. Kennedy loathe Lyndon B. Johnson?
US citizen traveling with Peruvian passport
Findminimum of Integral
Chilling water in copper vessel
How can I know how much authority/decision making power etc I have as an employee?
My previous employer committed a severe violation of the law and is also being sued by me. How do I explain the situation to future employers?
Run Bash scripts in folder all at the same time
This LM317 diagram doesn't make any sense to me
Did depressed people far more accurately estimate how many monsters they killed in a video game?
With a data transfer of 50 GB estimated 5 hours, are USB-C claimed speeds inaccurate or to blame?
Matrices with shadows
When do flights get cancelled due to fog?
What is the average number of draws it takes before you can not draw any more cards from the Deck of Many Things?
Appropriate conduit for several data cables underground over 300' run
Is there a formal/better word than "skyrocket" for the given context?
What factors could lead to bishops establishing monastic armies?
Is this really the Saturn V computer only, or are there other systems here as well?
Blocks from @ jafe
Magento 2: Get list of products on basis of attribute code
Get color values used in productsCode for filtering products by attributes?Get Products list with all product attributes in MagentoCan't get custom attribute from getUsedProductsCreate Dropdown Attribute for ProductsHow to list attributes for all SKUs in the system? My code only shows half productsGet product attribute on product list in one queryList products by dropdown attributeMagento 2.2 - How can I pass the color attribute from the category list to the product detail page?Get list of all modules using code in Magento 2
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Is there any way I can fetch list of products having a attribute_code
.
For eg:
Getting list of all products which has attribute color
product-attribute magento2.2
add a comment |
Is there any way I can fetch list of products having a attribute_code
.
For eg:
Getting list of all products which has attribute color
product-attribute magento2.2
add a comment |
Is there any way I can fetch list of products having a attribute_code
.
For eg:
Getting list of all products which has attribute color
product-attribute magento2.2
Is there any way I can fetch list of products having a attribute_code
.
For eg:
Getting list of all products which has attribute color
product-attribute magento2.2
product-attribute magento2.2
asked Oct 16 '17 at 9:37
DEEP JOSHIDEEP JOSHI
3561 gold badge5 silver badges24 bronze badges
3561 gold badge5 silver badges24 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can use below code to filter your product collection if the product has color attribute.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$productCollection = $objectManager->get("MagentoCatalogModelProduct")->getCollection();
$productCollection->addAttributeToSelect("color")->addAttributeToFilter(
array(
array('attribute'=> 'color','notnull' => true),
array('attribute'=> 'color','neq' => ''),
array('attribute'=> 'color','neq' => 'NO FIELD'),
),'','left'
);
$productIds = array();
foreach ($productCollection as $product)
$productIds[] = $product->getId();
print_r($productIds);
If you want to filter specific option of any attribute. (red option of color attribute), then you can simply filter your products as below.
$attributes = $objectManager->create('MagentoEavApiAttributeRepositoryInterface');
$color = $attributes->get(MagentoCatalogModelProduct::ENTITY, 'color');
$optionId = $color->getSource()->getOptionId('red');
$productCollection->addAttributeToSelect("color")->addAttributeToFilter('color',$optionId);
$productIds = array();
foreach ($productCollection as $product)
$productIds[] = $product->getId();
print_r($productIds);
Note : It's a good practice to use
objectManager
by dependency
injection.
Can you please elaborate your code?
– DEEP JOSHI
Oct 16 '17 at 10:14
Above code will filter those products which have not null or blank value in color attribute.
– Dinesh Yadav
Oct 16 '17 at 10:23
I am not getting list of product ids here.
– DEEP JOSHI
Oct 16 '17 at 10:26
post your code in question.
– Dinesh Yadav
Oct 16 '17 at 10:33
I need help with code to get product ids on from attribute code.
– DEEP JOSHI
Oct 16 '17 at 11:13
|
show 2 more comments
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');
);
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%2f197408%2fmagento-2-get-list-of-products-on-basis-of-attribute-code%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use below code to filter your product collection if the product has color attribute.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$productCollection = $objectManager->get("MagentoCatalogModelProduct")->getCollection();
$productCollection->addAttributeToSelect("color")->addAttributeToFilter(
array(
array('attribute'=> 'color','notnull' => true),
array('attribute'=> 'color','neq' => ''),
array('attribute'=> 'color','neq' => 'NO FIELD'),
),'','left'
);
$productIds = array();
foreach ($productCollection as $product)
$productIds[] = $product->getId();
print_r($productIds);
If you want to filter specific option of any attribute. (red option of color attribute), then you can simply filter your products as below.
$attributes = $objectManager->create('MagentoEavApiAttributeRepositoryInterface');
$color = $attributes->get(MagentoCatalogModelProduct::ENTITY, 'color');
$optionId = $color->getSource()->getOptionId('red');
$productCollection->addAttributeToSelect("color")->addAttributeToFilter('color',$optionId);
$productIds = array();
foreach ($productCollection as $product)
$productIds[] = $product->getId();
print_r($productIds);
Note : It's a good practice to use
objectManager
by dependency
injection.
Can you please elaborate your code?
– DEEP JOSHI
Oct 16 '17 at 10:14
Above code will filter those products which have not null or blank value in color attribute.
– Dinesh Yadav
Oct 16 '17 at 10:23
I am not getting list of product ids here.
– DEEP JOSHI
Oct 16 '17 at 10:26
post your code in question.
– Dinesh Yadav
Oct 16 '17 at 10:33
I need help with code to get product ids on from attribute code.
– DEEP JOSHI
Oct 16 '17 at 11:13
|
show 2 more comments
You can use below code to filter your product collection if the product has color attribute.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$productCollection = $objectManager->get("MagentoCatalogModelProduct")->getCollection();
$productCollection->addAttributeToSelect("color")->addAttributeToFilter(
array(
array('attribute'=> 'color','notnull' => true),
array('attribute'=> 'color','neq' => ''),
array('attribute'=> 'color','neq' => 'NO FIELD'),
),'','left'
);
$productIds = array();
foreach ($productCollection as $product)
$productIds[] = $product->getId();
print_r($productIds);
If you want to filter specific option of any attribute. (red option of color attribute), then you can simply filter your products as below.
$attributes = $objectManager->create('MagentoEavApiAttributeRepositoryInterface');
$color = $attributes->get(MagentoCatalogModelProduct::ENTITY, 'color');
$optionId = $color->getSource()->getOptionId('red');
$productCollection->addAttributeToSelect("color")->addAttributeToFilter('color',$optionId);
$productIds = array();
foreach ($productCollection as $product)
$productIds[] = $product->getId();
print_r($productIds);
Note : It's a good practice to use
objectManager
by dependency
injection.
Can you please elaborate your code?
– DEEP JOSHI
Oct 16 '17 at 10:14
Above code will filter those products which have not null or blank value in color attribute.
– Dinesh Yadav
Oct 16 '17 at 10:23
I am not getting list of product ids here.
– DEEP JOSHI
Oct 16 '17 at 10:26
post your code in question.
– Dinesh Yadav
Oct 16 '17 at 10:33
I need help with code to get product ids on from attribute code.
– DEEP JOSHI
Oct 16 '17 at 11:13
|
show 2 more comments
You can use below code to filter your product collection if the product has color attribute.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$productCollection = $objectManager->get("MagentoCatalogModelProduct")->getCollection();
$productCollection->addAttributeToSelect("color")->addAttributeToFilter(
array(
array('attribute'=> 'color','notnull' => true),
array('attribute'=> 'color','neq' => ''),
array('attribute'=> 'color','neq' => 'NO FIELD'),
),'','left'
);
$productIds = array();
foreach ($productCollection as $product)
$productIds[] = $product->getId();
print_r($productIds);
If you want to filter specific option of any attribute. (red option of color attribute), then you can simply filter your products as below.
$attributes = $objectManager->create('MagentoEavApiAttributeRepositoryInterface');
$color = $attributes->get(MagentoCatalogModelProduct::ENTITY, 'color');
$optionId = $color->getSource()->getOptionId('red');
$productCollection->addAttributeToSelect("color")->addAttributeToFilter('color',$optionId);
$productIds = array();
foreach ($productCollection as $product)
$productIds[] = $product->getId();
print_r($productIds);
Note : It's a good practice to use
objectManager
by dependency
injection.
You can use below code to filter your product collection if the product has color attribute.
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$productCollection = $objectManager->get("MagentoCatalogModelProduct")->getCollection();
$productCollection->addAttributeToSelect("color")->addAttributeToFilter(
array(
array('attribute'=> 'color','notnull' => true),
array('attribute'=> 'color','neq' => ''),
array('attribute'=> 'color','neq' => 'NO FIELD'),
),'','left'
);
$productIds = array();
foreach ($productCollection as $product)
$productIds[] = $product->getId();
print_r($productIds);
If you want to filter specific option of any attribute. (red option of color attribute), then you can simply filter your products as below.
$attributes = $objectManager->create('MagentoEavApiAttributeRepositoryInterface');
$color = $attributes->get(MagentoCatalogModelProduct::ENTITY, 'color');
$optionId = $color->getSource()->getOptionId('red');
$productCollection->addAttributeToSelect("color")->addAttributeToFilter('color',$optionId);
$productIds = array();
foreach ($productCollection as $product)
$productIds[] = $product->getId();
print_r($productIds);
Note : It's a good practice to use
objectManager
by dependency
injection.
edited Oct 17 '17 at 9:07
answered Oct 16 '17 at 10:06
Dinesh YadavDinesh Yadav
4,2641 gold badge9 silver badges37 bronze badges
4,2641 gold badge9 silver badges37 bronze badges
Can you please elaborate your code?
– DEEP JOSHI
Oct 16 '17 at 10:14
Above code will filter those products which have not null or blank value in color attribute.
– Dinesh Yadav
Oct 16 '17 at 10:23
I am not getting list of product ids here.
– DEEP JOSHI
Oct 16 '17 at 10:26
post your code in question.
– Dinesh Yadav
Oct 16 '17 at 10:33
I need help with code to get product ids on from attribute code.
– DEEP JOSHI
Oct 16 '17 at 11:13
|
show 2 more comments
Can you please elaborate your code?
– DEEP JOSHI
Oct 16 '17 at 10:14
Above code will filter those products which have not null or blank value in color attribute.
– Dinesh Yadav
Oct 16 '17 at 10:23
I am not getting list of product ids here.
– DEEP JOSHI
Oct 16 '17 at 10:26
post your code in question.
– Dinesh Yadav
Oct 16 '17 at 10:33
I need help with code to get product ids on from attribute code.
– DEEP JOSHI
Oct 16 '17 at 11:13
Can you please elaborate your code?
– DEEP JOSHI
Oct 16 '17 at 10:14
Can you please elaborate your code?
– DEEP JOSHI
Oct 16 '17 at 10:14
Above code will filter those products which have not null or blank value in color attribute.
– Dinesh Yadav
Oct 16 '17 at 10:23
Above code will filter those products which have not null or blank value in color attribute.
– Dinesh Yadav
Oct 16 '17 at 10:23
I am not getting list of product ids here.
– DEEP JOSHI
Oct 16 '17 at 10:26
I am not getting list of product ids here.
– DEEP JOSHI
Oct 16 '17 at 10:26
post your code in question.
– Dinesh Yadav
Oct 16 '17 at 10:33
post your code in question.
– Dinesh Yadav
Oct 16 '17 at 10:33
I need help with code to get product ids on from attribute code.
– DEEP JOSHI
Oct 16 '17 at 11:13
I need help with code to get product ids on from attribute code.
– DEEP JOSHI
Oct 16 '17 at 11:13
|
show 2 more comments
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');
);
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%2f197408%2fmagento-2-get-list-of-products-on-basis-of-attribute-code%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');
);
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');
);
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');
);
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