0

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);

or

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

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

Please help me, thanks all

  • Can you please explain what's issue in this code? – Rohan Hapani Jul 5 '19 at 5:30
  • Hi Rohan Hapani, i want load product or get id product by price or ean – TN98 Jul 5 '19 at 5:48
  • check it with : echo $productId->getId() for product id and print_r($productId->getData()); for product load. – Rohan Hapani Jul 5 '19 at 5:53
  • please check this magento.stackexchange.com/questions/279680/… – Shafeel Sha Jul 5 '19 at 6:24
0

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

| 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.