13

I added a new extension attribute extensionattribute1 to

Magento\Catalog\Api\Data\ProductInterface.

<attribute code="extensionattribute1" type="string">
  <join reference_table="training_extensionattributes_values" reference_field="product_id" join_on_field="entity_id">
    <field column="extensionattribute1">extensionattribute1</field>
  </join>
</attribute>

That worked all right and the extension attribute is shining up in the collection gathered over the rest api.

Now I would like to filter the product list returned by

Magento\Catalog\Api\ProductRepositoryInterface

programmatically via the SearchCriteriaBuilder (without passing the params via rest api). In concrete: I want to return it all products where extensionattribute1 is set to the value '20'.

What I tried was to build a filter like that:

   $filter=$this->filterBuilder
        ->setField('extensionattribute1')
        ->setValue('20')
        ->create();

But that did not work (Invalid attribute name Error). Same with

   $filter=$this->filterBuilder
        ->setField('extension_attributes[extensionattribute1]')
        ->setValue('20')
        ->create();

Thanks for giving me a hint on how to reach my goal.

6
  • 1
    Have you tried training_extensionattributes_values.extensionattribute1? May 2, 2016 at 15:22
  • Thank you for your idea. This does not work either, I am afraid. I would be very grateful if someone could enlighten me with another idea... Nov 21, 2016 at 11:42
  • Did you guys get any solutions? Feb 4, 2019 at 6:31
  • Hey, Can you please share the solution ?! Aug 22, 2019 at 18:34
  • If you found the solution please share.I am facing similar issue.
    – tru.d
    Nov 13, 2019 at 6:29

1 Answer 1

1

this method you used was fine as below:

$filter=$this->filterBuilder
        ->setField('extensionattribute1')
        ->setValue('20')
        ->create();

the change that I found to make the filter working was to add a filterProcessor so that you can declare the extension attribute filter as a custom filter

in the di.xml, declaring a virtual type does exactly this:

<virtualType name="Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor" type="Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor">
            <arguments>
                <argument name="customFilters" xsi:type="array">
                    <item name="training_extensionattributes_values" xsi:type="object">Mbs\ProductExtensionAttributes\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\TrainingExtensionattributesValuesFilter</item>
                </argument>
            </arguments>
    </virtualType> 

finally the filter code below does the filter for my situation.

class TrainingExtensionattributesValuesFilter implements \Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface
{
    /**
     * @inheritDoc
     */
    public function apply(Filter $filter, AbstractDb $collection)
    {
        $collection->getSelect()->where('extension_attribute_training_extensionattributes_values.extension_value1 like ?', $filter->getValue() . '%');
        return true;
    }
}

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.