I have a ReviewCollection, im trying to each "Review/Product" get a product image so i can pass on and display on template file:
public function getCollection()
{
//\Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $_reviewsColFactory,
$collection = $this->_reviewsColFactory->create()->addStoreFilter(
$this->_storeManager->getStore()->getId()
)->addStatusFilter(
\Magento\Review\Model\Review::STATUS_APPROVED
)->setDateOrder();
$arr = array();
foreach ($collection as $_review) {
$days = $this->returnDays($_review);
if ($days <= $this->_days) {
if (!isset($arr[$_review->getSku()])) {
$arr[$_review->getSku()]['cont'] = 1;
} else {
$arr[$_review->getSku()]['cont']++;
}
$arr[$_review->getSku()]['productUrl'] = $_review->getProductUrl();
$arr[$_review->getSku()]['productName'] = $_review->getName();
$arr[$_review->getSku()]['productImg'] = $_review->getImage(); //Empty
}
}
// arsort($arr);
return $collection;
}In that case is every $_review a Model/Product ?
Thanks
Solved! Go to Solution.
Ty for the answer, i found out that from $reviewCollection->getId returns me the productId, loading ProductRepositoryInterface i got the imageUrl as follow:
$imageBlock = $this->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct');
$productImage = $imageBlock->getImage($product, 'category_page_grid');
$productImage->toHtml();I uploaded the full code at my GitHub.
You can get item image based on sku from review collection,
just add dependency on __construct() method,
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
) {
$this->productRepository = $productRepository;
}
try {
$itemSku = $_review->getSku();
$productData = $this->productRepository->get($itemSku);
$itemImage = $productData->getData('image');
} catch (\Exception $exception) {
$productData = null;
return "Invalid sku";
};Set $itemImage inside your function and you got image name.
If issue resolved, Click kudos/Accept as solutions.
Ty for the answer, i found out that from $reviewCollection->getId returns me the productId, loading ProductRepositoryInterface i got the imageUrl as follow:
$imageBlock = $this->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct');
$productImage = $imageBlock->getImage($product, 'category_page_grid');
$productImage->toHtml();I uploaded the full code at my GitHub.