1

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...

Thanks

  • try $this->_productCollection->create() ->addAttributeToSelect('fabric') ->addAttributeToFilter('fabric', 'silk'); – Priyank May 15 '18 at 5:16
  • still not getting custom attribute collection – Mano 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
1

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);
        }
  • How to check particular product has Straps attribute value "Regular"?@Amit – Mano 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 at 12:46

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.