2

I want to get product collection based on attribute.

For ex : If "Size" attribute exist in "Bag","Bottom" attribute set then, All Product's of Bag and Bottom return in product collection.

How could i able to achieved that?

Any help would be appreciated.

3 Answers 3

2

You can't directly get product collection based on an attribute that exists in the attribute set.

First of all, You need to get all attributes set and from them, you can get all attributes from the attribute set.

After that, you can get product collection using the attribute set. It will automatically return the product collection of that attribute.

Add this below code in your block

<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\Helloworld\Block;

class CustProColl extends \Magento\Framework\View\Element\Template {

    /**
     * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
     */
    protected $collectionFactory;

    /**
     * @var \Magento\Catalog\Model\Product\AttributeSet\Options
     */
    protected $options;

    /**
     * @var \Magento\Eav\Api\AttributeManagementInterface
     */
    protected $attributeManagement;

    /**
     * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory  
     * @param \Magento\Catalog\Model\Product\AttributeSet\Options            $options            
     * @param \Magento\Eav\Api\AttributeManagementInterface                  $attributeManagement
     */
    public function __construct(
        ...
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory,
        \Magento\Catalog\Model\Product\AttributeSet\Options $options,
        \Magento\Eav\Api\AttributeManagementInterface $attributeManagement
        ...
    ) {
        ...
        $this->collectionFactory = $collectionFactory;
        $this->options = $options;
        $this->attributeManagement = $attributeManagement;
        ...
    }
    

    public function getProductColl() {
        $attributeSetArray = $this->options->toOptionArray();
        $attrSetIds = [];
        foreach ($attributeSetArray as $key => $value) {
            $attributes = $this->attributeManagement->getAttributes(\Magento\Catalog\Model\Product::ENTITY, $value['value']); // Get all attribute using attribute set id.
            foreach ($attributes as $attribute) {
                if ($attribute->getAttributeId() == 93) { // 93 is your attribute id which you want to check that exist in attribute set.
                    $attrSetIds[] = $value['value'];
                }
            }
        }
        $collection = $this->collectionFactory->create();
        $collection->addFieldToSelect('*');
        $collection->addFieldToFilter('attribute_set_id', ['in' => $attrSetIds]); // Return collection based on attribute set ids. It means your attribute exist in product.
        return $collection;
    }
}

I hope it's helpful for you.

2

Try Following way ..

<?php
namespace Rakesh\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{    
    protected $_productCollectionFactory;
        
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,        
        array $data = []
    )
    {    
        $this->_productCollectionFactory = $productCollectionFactory;    
        parent::__construct($context, $data);
    }
    
    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('size',  array('notnull' => true));
        return $collection;
    }
}
?>
3
  • It's not working. When Size value will not set at that time, that product will not return in collection. Commented Jun 30, 2020 at 4:12
  • @AnkitaPatel if size attribute not assign then product collection empty otherwise it return Commented Jun 30, 2020 at 4:13
  • Not working. For ex : If Joust Bag product's size value still not set, then it's not return in collection. I want that also. Attribute option not matter. Attribute should be exist in product. I mean in attribute set. Commented Jun 30, 2020 at 4:17
1

You can use the Product Collection factory as below.

\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory

$_collection = $this->_productCollectionFactory->create();
$_collection->addAttributeToSelect('*');
$_collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
// Your attribute code   
$_collection->addAttributeToFilter('size',  array('notnull' => true));

Get your product.

foreach ($_collection as $_product) {
    $_product->getName();
}
3
  • It's not working. When Size value will not set at that time, that product will not return in collection. Commented Jun 30, 2020 at 4:13
  • if the attribute is not set then also you want products? Commented Jun 30, 2020 at 4:14
  • Not working. For ex : If Joust Bag product's size value still not set, then it's not return in collection. I want that also. Attribute option not matter. Attribute should be exist in product. I mean in attribute set. Commented Jun 30, 2020 at 4:18

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.