0

I am trying to get all product ids of multi-select attribute "filter_type" ids($optionData) are assigned.

But I am unable to get them as its only works first time. In second and other loop I am not getting any products. However its working fine when I pass id manually in place of (int)$optionValues. e.g. 441 etc.

   $optionData = array('435','436','437','438','441','439');
    $productIds = array();
    foreach ($optionData as $optionValues) {
         $productIds[] = $_productCollection->addAttributeToFilter(
                    array(
                        array('attribute' => "filter_type", 'finset' => (int)$optionValues)
                    )
                )->getAllIds();          

    }
    echo "<pre>"; print_r($productIds);

`

2 Answers 2

1

It will be because you are adding a new filter each iteraction rather than replacing the current filter. Instead you can either reset the where query if you only have the 1 filter parameter or you can create a copy of the query each iteraction.

To reset the where query

foreach ($optionData as $optionValues) {
     $_productCollection->getSelect()->reset(\Magento\Framework\DB\Select::WHERE);
     $productIds[] = $_productCollection->addAttributeToFilter(
         array(array('attribute' => "filter_type", 'finset' => (int)$optionValues))
     )->getAllIds();          
}

Or to create a copy

foreach ($optionData as $optionValues) {
     $_tmpProdCollection = clone $_productCollection;
     $productIds[] = $_tmpProdCollection->addAttributeToFilter(
         array(array('attribute' => "filter_type", 'finset' => (int)$optionValues))
     )->getAllIds();    
     unset($_tmpProdCollection);      
}
1
  • Its working for me now, Thank You @Sam Commented Mar 5, 2020 at 12:35
0

Can you please try below code which can be help to resolved it.

$optionData = array('435','436','437','438','441','439');
$productIds = array();
foreach ($optionData as $optionValues) {
     $productIds[$optionValues][] = $_productCollection->addAttributeToFilter(
                array(
                    array('attribute' => "filter_type", 'finset' => (int)$optionValues)
                )
            )->getAllIds();          

}
echo "<pre>"; print_r($productIds);

Thanks, Ketan T.

2
  • Hi, @Ketan, I have tried your approach, Its still not working. Commented Mar 5, 2020 at 11:53
  • Ok let me check :) Commented Mar 5, 2020 at 15:26

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.