2

I created a custom drop down attribute in Magento2 from admin. I tried to collect all associated products by using filters. I am able to filter product collection based on text field attributes, for example

$productcollection = $this->_productCollection
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('sku', '24-MB01')
    ->load();

But when I try to filter using dropdown custom attribute value I'm getting an empty array

$productcollection = $this->_productCollection
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('tag', 'popular')
    ->load();

How am I supposed to filter product collection using a dropdown attribute. Can someone help with this?

  • thanks for your response. i'm looking drop down attribute product collection .Exp : attribute_code (tags) type->dropdown, drop down value (popular,new ......) i need collect all the popular products in the collection. – Suresh C Feb 7 '17 at 8:51
3

For dropdown attribute, We can filter using dropdown option ID instead of Label.

So your code is like below.

$productcollection = $this->_productCollection
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('tag', 28)
    ->load();

Above 28 is the ID of the popular option of tag dropdown attribute.

enter image description here

1

I assume the 'Tag' is your attribute you created in the admin and the value 'Popular' is a option label associated with one of the option id with attribute 'Tag' not the value itself. The value might be an integer number internally used by magento.

As per the answer here : Loading a product collection by a specific attribute drop-down value , It is not possible to filter collection based on the option label, however you can filter the collection based on attribute option id, i.e. the integer value. You can get the option id for an option label, in your case 'popular' from the model.

You can use the code from the answer in the link I mentioned above. I hope it helps you.

PS: I did not add this answer in comment simply because I CAN NOT. I do not have enough reputation to comments. So, instead of down voting for posting an answer instead of comment, post requirement at meta stack exchange to give ability to comment with lower reputation.

-1

please add this code in the your form.php file where you want to dropdown attribute

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->get('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()
    ->addAttributeToSelect('*');

$option[] = [
    'value' => '',
    'label' => __('-------- Please select a product --------'),
];

foreach ($collection as $key=>$product) {
    $option[] = [
        'value' => $product->getId(),
        'label' => $product->getName(),
    ];
}

$fieldset->addField(
    'product_id',
    'select',
    [
        'label' => __('Select Product'),
        'name' => 'product_id',
        'required'  => true,
        'values' => $option,
    ]
);
  • so many things wrong with this i can not even start. First of all you do not use objectmanager to inject classes. Furthermore you are getting a whole collection to next put it inside an option array instead of not using the option classes proviced by the sourcemodels of Magento to create the array. Last but not least you are using $fieldset that is not defined anywhere in your code. – CompactCode May 30 '18 at 4:56
  • @SwAt.Be Why its not good I am using this code its working for me – Rahul Katoch May 30 '18 at 5:53
  • Objectmanager is a bad way to load dependencies. Reasons you can find here : magento.stackexchange.com/questions/117098/… – CompactCode Jun 1 '18 at 4:10
  • The other things i already explained. You need to make a source model to get your data. Fieldsets and fields have nothing to do with his question since he did not specified where he wanted it. If you are implementing fieldsets to create new parts in a ui component you should follow the ui component XML structure. If you do not get this or then don not take my advice but i will not people spread bad coding practices. – CompactCode Jun 1 '18 at 4:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.