Magento 2 How to load product by id
1) Object method
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);
2) Factory Method
<?phpnamespace Hello\Module\Block;
class Product extends \Magento\Framework\View\Element\Template{ protected $_productloader;
public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Catalog\Model\ProductFactory $_productloader ) { $this->_productloader = $_productloader; parent::__construct($context); } public function getLoadProduct($id) { return $this->_productloader->create()->load($id); }}
If like efforts, Please share, comment and subscribe for future posts and inspire more.
Comments
Post a Comment