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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.