1

I am trying to filter product collection with custom attribute "Groups" code "group" which is a multi-select attribute created from Magento admin, but it is giving empty result (this attribute is assigned to products)

Code:

$collection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('group', array( 'in' => array( 5 ) ));
        //$collection->addAttributeToFilter(array( array('attribute'=>'group', 'eq' => '5')));
        $collection->load();

what am I doing wrong ?

Note: Excuse me for using Object manager as currently I am just trying functionality

| improve this question | | | | |
1

Finally got the solution

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$collection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$collection->addAttributeToSelect('*');
$collection->addFieldToFilter('group',
    array(
        array('finset'=> array(5))
    )
);
$collection->load();

used addFieldToFilterinstead of addAttributeToFilter

$collection->addFieldToFilter('group',
        array(
            array('finset'=> array(5))
        )
    );
| improve this answer | | | | |
0

You can try Like This

  $collection->addAttributeToSelect('*');
  $collection->addFieldToFilter('group', ['finset'=> 5]);
  $collection->load();

Hope it Helps.Happy Coding!

| improve this answer | | | | |

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.