3

I have a model which returns data to pool interface for section. Problem is that I'm not able to get product image URL with helper in this model, it always returns placeholder thumbnail URL instead of real image URL.

This is content of my class:

use \Magento\Catalog\Model\CategoryFactory;
use \Magento\Catalog\Helper\Image;
class UpsellProducts
{
 protected $categoryFactory;
 protected $imageHelper;
 public function __construct(
 CategoryFactory $categoryFactory,
 Image $imageHelper
 ) {
 $this->categoryFactory = $categoryFactory;
 $this->imageHelper = $imageHelper;
 }
 public function getCategory($id)
 {
 $category = $this->categoryFactory->create()->load($id);
 return $category;
 }
 public function getProductsCollection($id)
 {
 $r = array();
 $products = $this->getCategory($id)->getProductCollection()->addAttributeToSelect('*')->setPageSize(3);
 $products->getSelect()->orderRand();
 $products->load();
 $image = 'product_small_image';// or 'category_page_list';
 foreach ($products as $product) {
 $r[] = $product->getData();
 $r['image'][] = $this->imageHelper->init($product, $image)->getUrl();
 }
 return $r;
 }
 public function getUpsellProducts()
 {
 // @TODO category id from BE config
 $t = $this->getProductsCollection(876);
 return $t;
 }
}

I'm using default Image helper and loading 3 random products from category, when I try to get image for this product, I get thumbnail placeholder URL instead of product image.

asked Apr 29, 2016 at 13:12

1 Answer 1

1

You can take a look at this post at my answer or Frank's.

Maybe one of the ways posted there could help you.

Images returning as placeholders - custom product collection

answered Jun 7, 2016 at 8:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.