Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,774,165 members, 6,607,377 topics. Date: Sunday, 28 November 2021 at 06:32 PM

How To Load Product By SKU In Magento 2? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / How To Load Product By SKU In Magento 2? (661 Views)

Opencart Product Options SKU And UPC / How To Override A Core Class In Magento 2 / How To Wait For Page To Load/ready In Selenium Webdriver (2) (3) (4)

(1) (Reply)

How To Load Product By SKU In Magento 2? by alex288: 11:55am On Nov 22, 2019
Programmatically you can use any other approach to get the same results.
You can load product by SKU with two methods.
• Object Manager
• Factory Method
Object Manager
A short method but nor recommended but Magento.
$sku =”ABC”;
$objectManager = \Magento\Forum\App\ObjectManager::getInstance();
$product = $objectManager->get(‘Magento\Catalog\Model\Product’)->loadByAttribute(‘sku’,$sku);
Factory Method
Recommended method by Magento.
<?php
namespace FME\Module\Block;

class Product extends \Magento\Framework\View\Element\Template
{

protected $_productloader;


public function __construct(
\Magento\Catalog\Model\ProductFactory $_productloader


) {


$this->_productloader = $_productloader;

}
public function getLoadProduct($sku)
{
return $this->_productloader->create()->loadByAttribute('sku', $sku);
}


}

phtml File Code
$product = $this ->getLoadProduct(“ABC”);
echo $product->getName();


Complete Tutorial on Magento 2 Load Products by SKU https://www.fmeextensions.com/blog/magento-2-load-product-by-sku/

(1) (Reply)

How To Set Up Mysql Server Configuration: / US, UK, Canada Senior Software Developer Jobs / Insight About Mobile App Development, And How DWTA Is Offering Practical Study

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2021 Oluwaseun Osewa. All rights reserved. See How To Advertise. 20
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.