As you know there are so many alternate methods to get product details by given id in Magento 1.x. This code snippet will be very useful at many custom development with Magento.
Lets check the code used in Magento 1.x.
1 2 | $productId = 10 // Assign product id here.. $_product = Mage::getModel('catalog/product')->load($productId); |
List of some methods to get various product details like Short description, Name, Price and Product URL etc.
1 2 3 4 5 6 | echo $_product->getShortDescription(); //product's short description echo $_product->getDescription(); // product's long description echo $_product->getName(); //product name echo $_product->getPrice(); //product's regular Price echo $_product->getSpecialPrice(); //product's special Price echo $_product->getProductUrl(); //product url |
Now lets see how to get product details by id in Magento 2.x. You can load the product by product id simply as below
1 2 3 | $product_id = 10; // Assign product id here $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id); |
Leave a Reply