0

I would like to know. How can i filter collection by attributes value as I am making filter api for app. Please check below code where i am filter the collection this is working for simple products but for configuration i'm facing an issue

   foreach ($productInfo['attrs'] as $attr) {

                if ($attr->code == 'price') {
                    foreach ($attr->values as $key => $value) {
                            $rr[] = explode("-", $value);
                    }
                      $min = min(array_column($rr, '0'));
                      $max = max(array_column($rr, '1'));
                    $collection->addAttributeToFilter('price', ['from' => $min, 'to' => $max]);
                }
                   if ($attr->code != 'price' && $attr->code != 'cat' ) {
                        $collection->addAttributeToFilter($attr->code, array('eq' => $attr->values));

                    }
                    if ($attr->code == 'cat') {
                       // print_r($attr->values);
                         $collection->addCategoriesFilter(['in' =>$attr->values ]);
                    }
                     if ($attr->code == 'ram') {
                       // print_r($attr->values);
                         $collection->addFieldToFilter('ram', ['in' => $attr->values]);
                    }

            }

where I am receiving the value on this format

{"customer_id":"10","category_id":"3","page":"1","sort":"1", "attrs":[
        {
                "code":"price","values":["1000-30000"]
        },
         {
                "code":"ram","values":["6","7"]
        },
        {
                "code":"color","values":["12"]
        },
          ]}

1 Answer 1

0

I assumed your issue is that you can't filter the configurable products with the ram attribute. The reason is likely because the ram is only defined against the simple products.

If this is the case, you'd need to filter your collection with an additional function that would loop in your collection results and check for each configurable if one of its simple product has a ram that is in your filter params.

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.