How to get attribute image
in Magento2 using product object?
2 Answers
Just inject this \Magento\Catalog\Api\ProductRepositoryInterfaceFactory
in your constructor.
protected $_productRepositoryFactory;
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterfaceFactory $productRepositoryFactory
) {
$this->_productRepositoryFactory = $productRepositoryFactory;
}
Get the image this way:
$product = $this->_productRepositoryFactory->create()->getById($item->getProductId());
$product->getData('image');
reference Get product image
Try Below code.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($_product->getId());
$image= $product->getData('attr_code');