1

I have custom dropdown attribute with yes and no values. I want to get only those product names which have attribute value yes. I am trying to avoid using object manager directly, so I need to accomplish this using dependencies. Any ideas?

1
  • on list page Or somewhere else ?
    – Pawan
    Feb 26, 2019 at 4:34

1 Answer 1

3

Using object manager

    $productcollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')
                                        ->addAttributeToSelect('*')
                                        ->addAttributeToSelect('custom_attribute','1');

use echo '<pre>';print_r($productcollection->getData()); to get product datas

Using constructor method

protected $_productRepository;

public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
) {
    $this->_productRepository = $productRepository;
}

punblic function getProductCollection(){
       $productcollection = $this->_productRepository->addAttributeToSelect('*')
            ->addAttributeToSelect('custom_attribute','1');
       return $productcollection;
}

use echo '<pre>';print_r($this->getProductCollection()->getData()); to get product datas

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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