Skip to content

How I can retrieve the Image Url of a given product?

I have a custom block in magento 2.4 where I retrieve a image List

namespace PcmagasFirstModuleBlockMyList;

use MagentoCatalogModelProductRepository;
use MagentoFrameworkApiSearchCriteriaBuilder;
use MagentoFrameworkViewElementTemplate;

class MyList extends Template
{
    private ProductRepository $productRepository;

    private SearchCriteriaBuilder $searchCriteriaBuilder;

    public function __construct(
        TemplateContext $context,
        ProductRepository $productRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->productRepository = $productRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }

    public function getRelatedProducts()
    {
        $skuList = ['24-MB01','24-MB04','24-UG01'];
        $searchCriteria = $this->searchCriteriaBuilder->addFilter('sku', $skuList, 'in')->create();

        return $this->productRepository->getList($searchCriteria)->getItems();
    }
}

And upon my phtml file I list the products like this:


    <?php foreach ($products as $product): ?>
        <div>
            <-- Display Image Here -->
            <h4>
                <?=$product->getName(); ?>
            </h4>
        </div>
    <?php endforeach; ?>

But if I try to do:


<?=$product->getImage(); ?>

I will get the path towards the image and NOT the actual Image. Is there a way to get the Image URL.