Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,238 members, 7,807,804 topics. Date: Wednesday, 24 April 2024 at 07:31 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? (801 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)

Udemy Computer Courses Combo Pack / Phoneclean Pro 5.5.0 Mac Crack / Web-designing Tutorial In Ibadan

(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 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 13
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.