Timeline for How to load product by SKU in magento 2
Current License: CC BY-SA 3.0
9 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Feb 22, 2021 at 16:39 | comment | added | 00-BBB | @Himanshu To get the exception to work it needs to be like: catch (\Magento\Framework\Exception\NoSuchEntityException $e) { ... | |
Mar 10, 2020 at 14:30 | comment | added | treyBake | This gives me a memory limit fatal error (mem limit = 2G) :S | |
Nov 6, 2018 at 5:21 | comment | added | Himanshu | @FabianSchmengler have tried to catch the exception but it is still breaking the same. | |
Nov 5, 2018 at 17:00 | comment | added | Fabian Schmengler | @Himanshu catch the exception. And if you need a fresh product instance in that case, you can still create it using the factory | |
Nov 5, 2018 at 6:20 | comment | added | Himanshu |
@FabianSchmengler yes recommendation of Repository is good as @RiccardoT answer. but what if i entered wrong sku then it will break whole operation & throw exception, so in this case we must have to depend on productFactory
|
|
Jan 31, 2018 at 7:24 | comment | added | Slimshadddyyy |
@FabianSchmengler: Tried using $product = $this->productFactory->create(); $product->load($product->getIdBySku($sku)); $product->getThumbnailUrl() to display product thumbnail image, but its not working.
|
|
Apr 18, 2016 at 0:02 | comment | added | nevvermind |
Also, your method will return a Product model, whilst using the Repository will give you a Product data model (Api/Data/Product ), which is a Product model converted into a dumbed-down DTO. Something to consider, as they're quite different.
|
|
Apr 17, 2016 at 20:46 | comment | added | Fabian Schmengler |
Actually while this is still working, using load() and collections is the "Magento 1" way, better use the repository as suggested by @RiccardoT.
|
|
Apr 17, 2016 at 20:41 | history | answered | Fabian Schmengler | CC BY-SA 3.0 |