magento get backend product data code example

Example 1: magento 2 get product image

use Magento\Framework\App\ObjectManager;
use Magento\Framework\View\Element\Template;
use Magento\Catalog\Model\Product;

class myClass extends Template
{	
	/**
	 * @param \Magento\Catalog\Model\Product $product
	 * @return \Magento\Catalog\Block\Product\Image
	 */
	public function getProductImage(Product $product){
		$objectManager =\Magento\Framework\App\ObjectManager::getInstance();
		/** @var \Magento\Catalog\Block\Product\ImageBuilder $imageBuilder */
		$imageBuilder = $objectManager->create(\Magento\Catalog\Block\Product\ImageBuilder::class);
		return $imageBuilder->create($product, 'category_page_grid');
	}
}

// in phtml use
getProductImage($product)?>

Example 2: magento load product by id

$productId = 20;
$product = Mage::getModel('catalog/product')->load($productId);

Tags:

Misc Example