I want get product by custom attribute

My code :

protected $product;

public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Magento\Catalog\Model\Product $product
    $this->product = $product;
    return parent::__construct($context);
$productId = $this->product->loadByAttribute('ean', 123456);


$productId = $this->product->loadByAttribute('price', 100);

Example : Get id product by price, ean, color ...

Please help me, thanks all


I suggest to take a look at collections in Magento 2. You can create a filter that you can use on the collection to filter the products you need based on your (custom) attributes. The collection contains the products you need, like in your example all products who has a specific color or a product with a specific ean.

Have a look at the following link;

Magento 2: Product collection with filters

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.