I have added the Straps attribute for product...

enter image description here

I want to filter out the attribute collection...

    public function offer()
      $productcollection = $this->_productCollection->create()
                ->addAttributeToSelect('*') ->addAttributeToFilter('staps', 'regular');            
      echo count($productcollection);exit;

But I can't get the answer.....

How to do that...


  • try $this->_productCollection->create() ->addAttributeToSelect('fabric') ->addAttributeToFilter('fabric', 'silk'); – Priyank May 15 '18 at 5:16
  • still not getting custom attribute collection – Mahi M May 15 '18 at 5:21
  • have you checked in inspect element is silk option value is silk? – Prashant Valanda May 15 '18 at 5:22
  • Please answer your question with the solution and mark it as accepted – Vivek Kumar May 15 '18 at 5:35

As per as, my screenshot, fabric is a multi-select attribute, So i can not filter a product collection by multi-Select attribute's option label, i should use that option's id.

    protected $attributeRepository;
    public function __construct(
        \Magento\Catalog\Model\Product\Attribute\Repository $attributeRepository
     $this->attributeRepository = $attributeRepository;

    public functin getOptionIdbyAttributeCodeandLabel($attr_code,$optionText)
       $attribute =$this->attributeRepository->get($attr_code);
      $optionId = $attribute->getSource()->getOptionId($optionText) 
       return $optionId;

     public function offer()
          $optionId = $this->getOptionIdbyAttributeCodeandLabel('fabric', 'silk')
          $productcollection = $this->_productCollection->create()
                    ->addAttributeToSelect('fabric') ->addAttributeToFilter('fabric',  $optionId);            
          echo count($productcollection);
return  count($productcollection);
| improve this answer | |
  • How to check particular product has Straps attribute value "Regular"?@Amit – Mahi M May 15 '18 at 8:54
  • @Amit, many syntax error in code, function name is not correct, semicolon missing in many places – jafar pinjar Jan 24 '19 at 12:46
  • Hello, How can I use multiple select options multiple filter in one phtml. e.g. First I want to show silk products, below them "regular" products and etc. How can I do that in list.phtml with collection filter ? Its only working for one time in foreach loop. – Dinesh Rajput Mar 2 at 14:05

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.