Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,207,646 members, 7,999,835 topics. Date: Monday, 11 November 2024 at 02:17 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? (844 Views)

Opencart Product Options SKU And UPC / How To Override A Core Class In Magento 2 / 6 Types Of Products In Magento E-commerce (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)

A+ Certification / Learning This SKILL Is Better Than Having A DEGREE / New Post Replying Yours

(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. 4
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.