0

I'm trying to write an observer and send product data to external source.

Have everything done except that when I get image it shows default Magento image.

public function __construct(
    \Magento\Sales\Api\Data\OrderInterface $order,
    \Magento\Catalog\Model\Product $product,
    \Magento\Catalog\Helper\Image $imgHelper
) {
     $this->_order = $order;
     $this->_product = $product;
     $this->_imgHelper = $imgHelper;
}

public function execute(\Magento\Framework\Event\Observer $observer){
    ...
    $itemCollection = $order->getItemsCollection();
    foreach($itemCollection as $item){
        $product = $this->_product->load($item->getData('product_id'));
        $this->_imgHelper->init($product, 'product_thumbnail_image')->getUrl();
    }
    ....
}

The image which I get is:

http://convert.wemessage.it/magento2/pub/media/catalog/product/cache/dc09e1c71e492175f875827bcbf6a37

Do I miss something?

2 Answers 2

0

First please check whether product object is loaded successfully or not by fetching id using $product->getId() if product object is fine then Try to use this

$this->_imgHelper->init($product, 'product_thumbnail_image')->setImageFile($product->getFile())->getUrl();

instead of this

$this->_imgHelper->init($product, 'product_thumbnail_image')->getUrl();

I hope this will help

7
0

damn it seem to be i had error in external source, limit of file name there was 100 chars..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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