Magento2 : get image path of product
Get the image URL with
$image->getImageUrl();
or if you want to output it as element:
echo $image->toHtml();
OR Try this :
$store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();
$imageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();
Note: you need to create objectmanager if use last solution.
Please use the code below in your template file :
$imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct');
$productImage = $imageBlock->getImage($product, 'category_page_grid'); /* $product pass product object here */
<a href="<?php echo $product->getProductUrl(); ?>"><?php echo $productImage->toHtml() ?></a> asdasdfs
One thing you need to keep in mind when getting a custom collection of products is how you filter the collection to have a values you need to be called on the front end. You say you have a custom page, so i will assume that you are creating a custom collection as well.
When you do you have to filter out what you will need. Inside you're block class, you will need something like this:
<?php
namespace Vendor\Namespace\Block;
use Magento\Catalog\Model\Product;
class Custompage extends \Magento\Framework\View\Element\Template {
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
array $data = []
){
$this->_productCollectionFactory = $productCollectionFactory;
$this->_catalogProductVisibility = $catalogProductVisibility;
parent::__construct($context, $data);
}
public function getProductCollection() {
$attrId = $this->getAttrId();
$collection = $this->_productCollectionFactory->create();
$collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());
$collection->addFieldToSelect('name');
$collection->addFieldToSelect('price');
$collection->addFieldToSelect('small_image');
return $collection;
}
}
Take note that we have $collection->addFieldToSelect('small_image');
as a field to select from. If you don't do this, when you pass the call to getImage()
the product object won't have the image url (and this is hard to see as the m2 objects are huge and hard to var_dump
). So you would end up with a value of NULL
coming back when you call for the image url.
Then in your template you can use:
<?php $productCollection = $block->getProductCollection(); ?>
<?php $imageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct'); ?>
<?php if (count($productCollection)): ?>
<?php foreach ($productCollection as $product): ?>
<?php $productImage = $imageBlock->getImage($product, 'category_page_grid'); ?>
<a href="<?php /* @escapeNotVerified */ echo $product->getProductUrl() ?>" class="product photo product-item-photo" tabindex="-1"><?php echo $productImage->toHtml(); ?></a>
<?php endforeach; ?>
<?php endif; ?>