I need to get a product but I only have a custom attribute (EAN), no ID, no SKU.

Is there a way to get a product by custom attribute in M2? The error I get when trying is this:

Fatal error: Uncaught TypeError: Argument 1 passed to
must be an instance of Magento\Eav\Model\Entity\AbstractEntity,
instance of Magento\Catalog\Model\ResourceModel\Product\Flat given,
called in
on line 1369

and defined in /home/www/olesiejuk/www/dev.olesiejuk.pl/vendor/magento/module-catalog/Model/ResourceModel/Product/Collection.php on line 1532

Thank you!

UPDATE (add code):

//[step] get product ID
    $filter = $this->filterFactory->create();



    $productCriteria = $this->searchCriteriaBuilder->create();

    $product = $this->product->loadByAttribute('ean', $value);

    $products = $this->productRepository->getList($productCriteria)->getItems();
  • But why you can't get any id or sku? – Alin Lupoiu Jun 30 '17 at 9:28
  • i think you can create a direct sql and searh in DB your for that EAN, and get the id or sku – Alin Lupoiu Jun 30 '17 at 9:29
  • I can't get the ID or SKU, because it's a search field where a customer can search using EAN/ I tried using loadByAttribute() from Product model, but it always return product with ID = 1... Any ideas apart from SQL or getting SKU/ID? – Mateusz Serotiuk Jun 30 '17 at 9:44

For anyone also running into this error, make sure used_in_product_listing is set to true for the attribute you're trying to filter on.

| improve this answer | |
  • Had the same issue. This fixes the problem. Thanks :) – floorz Jun 26 '19 at 20:13

Please use the below class to create object for searchCirteriaBuilder and use your custom attribute as addFilter to fetch the items match the searchcirteria.

        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
        \Magento\Framework\Api\FilterBuilder $filterBuilder,
        \Magento\Catalog\Model\ProductRepository $productRepository

        $filters[] = $this->filterBuilder


        $productCriteria = $this->searchCriteriaBuilder->create();

        $products = $this->productRepository->getList($productCriteria);

        $items = $products->getItems();

        foreach($items as $item) {

        $productId = $item->getId();
        $sku = $item->getSku();

| improve this answer | |
  • Tried exactly that, got the error refferenced above. – Mateusz Serotiuk Jun 30 '17 at 10:44
  • Could share your entire code. – Iyappan Perumal Jun 30 '17 at 11:06
  • added in original post – Mateusz Serotiuk Jun 30 '17 at 11:26
  • @MateuszSerotiuk I updated my answer I tried my self the answered code it works fine, i don't face any error message like what you mentioned, could you try the edited answer. Hope it will resolve your issue. – Iyappan Perumal Jul 1 '17 at 16:08
  • Thanks for your input there! I ended up convincing the client to change business logic instead of coding this one - just a simpler way to do it. Thank you again for your help! – Mateusz Serotiuk Jul 10 '17 at 20:24

I had similar issue and resolved by using following pattern:

    ['status' => 'catalog_product_entity_varchar'],
    'e.row_id = status.row_id AND status.attribute_id = 202',
->where('status.store_id = 1')
->where("status.value ='test'");
| improve this answer | |

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.